Page MenuHomeDevCentral
Paste P367

bak-mysql.sh
ActivePublic

Authored by dereckson on Oct 26 2024, 23:42.
Referenced Files
F3695553: bak-mysql.sh
Oct 26 2024, 23:44
F3695546: bak-mysql.sh
Oct 26 2024, 23:42
Subscribers
None
#!/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

Event Timeline

dereckson updated the paste's language from autodetect to bash.Oct 26 2024, 23:44
dereckson edited the content of this paste. (Show Details)
dereckson added projects: Servers, DBA.