Skip to content

Instantly share code, notes, and snippets.

@strangeman
Last active October 12, 2015 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strangeman/4069579 to your computer and use it in GitHub Desktop.
Save strangeman/4069579 to your computer and use it in GitHub Desktop.
simple script for backup mysql databases
#!/bin/bash
#create user for dump
#mysql> GRANT SHOW DATABASES, SELECT, LOCK TABLES, RELOAD ON *.* to dump@localhost IDENTIFIED BY 'dumppwd';
#mysql> FLUSH PRIVILEGES;
BACKUP_PATH=/mnt/mysqlbackup/dump
DBASE[0]=base1
DBASE[1]=base2
DBASE[2]=base3
COUNT=${#DBASE[@]}
# make backup directory
mkdir -p $BACKUP_PATH
# remove backup's older than 14 days
find $BACKUP_PATH -type f -ctime +13 -exec rm {} \;
#create dumps in cycle
INDEX=0
while [ "$INDEX" -lt "$COUNT" ]; do
# make and archive database backup
BACKUP_FILE=$BACKUP_PATH/${DBASE[$INDEX]}-`date +%Y-%m-%d`.sql.bz2
mysqldump \
--skip-set-charset \
--user=dump \
-pdumppwd \
${DBASE[$INDEX]} | \
bzip2 -9 > $BACKUP_FILE
echo $BACKUP_FILE created
INDEX=`expr $INDEX + 1`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment