Page MenuHomeDevCentral

bak-mysql.sh

Authored By
dereckson
Sat, Oct 26, 23:44
Size
1 KB
Referenced Files
None
Subscribers
None

bak-mysql.sh

#!/bin/sh
# -------------------------------------------------------------
# Backup MariaDB or MySQL databases
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Project: Nasqueron
# License: BSD-2-Clause
# -------------------------------------------------------------
set -e
BACKUP_DIR=/var/backups/db
# -------------------------------------------------------------
# Ensure user is root
#
# Note: POSIX shells don't always define $UID or $EUID.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
echo "This command must be run as root." >&2
exit 1
fi
# -------------------------------------------------------------
# Collect information
#
# :: Databases to dump
# :: Timestamp
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
DATABASES=$(mysql -Ns -e "show databases" | grep -v -E 'information_schema|performance_schema|mysql-innodb-')
SUBDIR=$(hostname -s)-$(date +"%s")
# -------------------------------------------------------------
# Backup
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mkdir "$BACKUP_DIR/$SUBDIR"
for database in $DATABASES; do
mysqldump $database | gzip > "$BACKUP_DIR/$SUBDIR/$database.sql.gz"
done

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2218851
Default Alt Text
bak-mysql.sh (1 KB)

Event Timeline