Skip to content

Instantly share code, notes, and snippets.

@admackin
Last active October 25, 2022 08:54
Show Gist options
  • Save admackin/1136210 to your computer and use it in GitHub Desktop.
Save admackin/1136210 to your computer and use it in GitHub Desktop.
Rolling MySQL database backups
#!/bin/sh
# use with cron entries such as:
#3 */2 * * * $HOME/bin/mysql-backup.sh dbname backupname hourly H
#8 1 * * * $HOME/bin/mysql-backup.sh dbname backupname daily a
#33 2 1,8,15,23 * * $HOME/bin/mysql-backup.sh dbname backupname weekly d
#33 2 28 * * $HOME/bin/mysql-backup.sh dbname backupname monthly b
DB_NAME="$1"
BACK_ROOT="$2"
PERIOD_NAME="$3"
DATE_FMT="$4"
DB_PASSWORD="password1234"
DB_USER="backup"
DUMP_NAME="${BACK_ROOT}/${PERIOD_NAME}/${DB_NAME}.`date +%${DATE_FMT}`.sql"
DB_SERVER="127.0.0.1"
nice mysqldump -u $DB_USER -p$DB_PASSWORD -h $DB_SERVER $DB_NAME | gzip -1 --rsyncable > "$DUMP_NAME.gz"
@rcusnir
Copy link

rcusnir commented Oct 25, 2022

Please add automatic directory tree creation line 16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment