├── README.md ├── backupmysql.conf └── backupmysql.sh /README.md: -------------------------------------------------------------------------------- 1 | Backup Your MySQL Databases to Disk, FTP, and Email 2 | =========== 3 | 4 | This is a shell script that backs up your MySQL servers. It can talk to as many servers as you'd like (remote or local), and has some features to filter out specific databases and table names. 5 | 6 | ### Requirements 7 | To backup databases locally, you'll only need bash and gzip, which ship with just about all Linux distros. 8 | 9 | To backup to FTP, you'll need the 'ftp' program. 10 | 11 | To backup to email, you'll need the 'mutt' program. 12 | 13 | ### Available options 14 | #### MySQL Settings 15 | ##### DBNAMES 16 | Databases you want to backup, separated by a space; leave empty to backup all databases on this host. 17 | ###### Example: 18 | `DBNAMES[0]="db1 db2"` 19 | 20 | ##### DBUSER 21 | Your MySQL username. 22 | ###### Example: 23 | `DBUSER[0]="root"` 24 | 25 | ##### DBPASS 26 | Your MySQL password. 27 | ###### Example: 28 | `DBPASS[0]="password"` 29 | 30 | ##### DBHOST 31 | Your MySQL server's location (IP address is best). 32 | ###### Example: 33 | `DBHOST[0]="localhost"` 34 | 35 | ##### DBTABLES 36 | Tables you want to backup or exclude, separated by a space; leave empty to back up all tables. 37 | ###### Example: 38 | `DBTABLES[0]="db1.table1 db1.table2 db2.table1"` 39 | 40 | ##### DBTABLESMATCH 41 | If you set this to 'include', it will backup ONLY the tables in DBTABLES, 'exclude' will backup all tables BUT those in DBTABLES. 42 | ###### Example: 43 | `DBTABLESMATCH[0]="include"` 44 | 45 | ##### DBOPTIONS 46 | If you want to give `mysqldump` other options, include them here. 47 | ###### Example: 48 | `DBOPTIONS[0]="--quick --single-transaction"` 49 | 50 | #### Email Settings 51 | ##### EMAILS 52 | Email addresses to send backups to, separated by a space. 53 | ###### Example: 54 | `EMAILS="address@yahoo.com address@usa.com"` 55 | 56 | ##### SUBJECT 57 | This is the subject of the email that you'll get. 58 | ###### Example: 59 | `SUBJECT="MySQL backup on $SERVER ($DATE)"` 60 | 61 | #### FTP Settings 62 | ##### FTPHOST 63 | ###### Example: 64 | `FTPHOST[0]="ftphost"` 65 | 66 | ##### FTPUSER 67 | ###### Example: 68 | `FTPUSER[0]="username"` 69 | 70 | ##### FTPPASS 71 | ###### Example: 72 | `FTPPASS[0]="password"` 73 | 74 | ##### FTPDIR 75 | ###### Example: 76 | `FTPDIR[0]="backups"` 77 | 78 | ### Backing up multiple servers 79 | For each server you're backing up, you will need to copy/paste the block under `MySQL Settings` and increment the key in brackets (e.g. the first server will be 0, the next will be 1, and so on). -------------------------------------------------------------------------------- /backupmysql.conf: -------------------------------------------------------------------------------- 1 | # Ameir Abdeldayem 2 | # http://www.ameir.net 3 | # You are free to modify and distribute this code, 4 | # so long as you keep my name and URL in it. 5 | 6 | # your MySQL server's name 7 | SERVER=`hostname -f` 8 | 9 | # directory to backup to 10 | BACKDIR=~/backups/mysql 11 | 12 | # date format that is appended to filename 13 | DATE=`date -u +'%F-%T'` 14 | 15 | #----------------------MySQL Settings--------------------# 16 | 17 | DBNAMES[0]="db1 db2" # databases you want to backup, separated by a space; leave empty to backup all databases on this host 18 | DBUSER[0]="root" # MySQL username 19 | DBPASS[0]="password" # MySQL password 20 | DBHOST[0]="localhost" # your MySQL server's location (IP address is best) 21 | DBPORT[0]="3306" # MySQL port 22 | DBTABLES[0]="db1.table1 db1.table2 db2.table1" # tables you want to backup or exclude, separated by a space; leave empty to back up all tables 23 | DBTABLESMATCH[0]="include" # include will backup ONLY the tables in DBTABLES, exclude will backup all tables BUT those in DBTABLES 24 | DBOPTIONS[0]="--quick --single-transaction" 25 | 26 | #----------------------Mail Settings--------------------# 27 | 28 | # set to 'y' if you'd like to be emailed the backup (requires mutt) 29 | MAIL=n 30 | 31 | # email addresses to send backups to, separated by a space 32 | EMAILS="address@yahoo.com address@usa.com" 33 | 34 | SUBJECT="MySQL backup on $SERVER ($DATE)" 35 | 36 | #----------------------Duplicity Settings--------------------# 37 | 38 | DUPLICITY=n 39 | DUPLICITY_OPTIONS="--no-encryption -v8 --s3-use-new-style" 40 | DUPLICITY_TARGET_URL="s3+http://my-backups/db/" 41 | 42 | #----------------------S3 Settings--------------------# 43 | 44 | S3_UPLOAD=n 45 | S3_PATH="my-backups/db/" 46 | S3_OPTIONS="" 47 | AWS_CLI_OPTIONS="--region us-east-1" 48 | 49 | #----------------------FTP Settings--------------------# 50 | 51 | # set "FTP=y" if you want to enable FTP backups 52 | FTP=n 53 | 54 | # FTP server settings; group each remote server using arrays 55 | # you can have unlimited remote FTP servers 56 | FTPHOST[0]="ftphost" 57 | FTPUSER[0]="username" 58 | FTPPASS[0]="password" 59 | FTPDIR[0]="backups" 60 | FTPPORT[0]="21" 61 | 62 | # directory to backup to; if it doesn't exist, file will be uploaded to 63 | # first logged-in directory; the array indices correspond to the FTP info above 64 | 65 | #-------------------Deletion Settings-------------------# 66 | 67 | # delete old files? 68 | DELETE=n 69 | 70 | # how many days of backups do you want to keep? 71 | DAYS=30 72 | 73 | #-------------------Compression Settings-------------------# 74 | 75 | COMPRESSION_COMMAND="gzip -f -9" 76 | COMPRESSION_EXTENSION="gz" 77 | #----------------------End of Settings------------------# 78 | -------------------------------------------------------------------------------- /backupmysql.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Ameir Abdeldayem 4 | # http://www.ameir.net 5 | # You are free to modify and distribute this code, 6 | # so long as you keep my name and URL in it. 7 | 8 | #----------------------Start of Script------------------# 9 | 10 | function die () { 11 | echo >&2 "$@" 12 | exit 1 13 | } 14 | 15 | CONFIG=${1:-`dirname $0`/backupmysql.conf} 16 | [ -f "$CONFIG" ] && . "$CONFIG" || die "Could not load configuration file ${CONFIG}!" 17 | 18 | # check of the backup directory exists 19 | # if not, create it 20 | if [ ! -d $BACKDIR ]; then 21 | echo -n "Creating $BACKDIR..." 22 | mkdir -p $BACKDIR 23 | echo "done!" 24 | fi 25 | 26 | for KEY in "${!DBHOST[@]}"; do 27 | echo "Backing up MySQL database on ${DBHOST[$KEY]}..." 28 | 29 | if [ -z "${DBNAMES[$KEY]}" ]; then 30 | echo -n "Creating list of all your databases..." 31 | DBS=`mysql -h ${DBHOST[$KEY]} --user=${DBUSER[$KEY]} --password=${DBPASS[$KEY]} -Bse "show databases;"` 32 | echo "done!" 33 | else 34 | DBS=${DBNAMES[$KEY]} 35 | fi 36 | 37 | # filter out the tables to backup 38 | if [ -n "${DBTABLES[$KEY]}" ]; then 39 | if [ ${DBTABLESMATCH[$KEY]} = "exclude" ]; then 40 | TABLES='' 41 | for table in ${DBTABLES[$KEY]}; do 42 | TABLES="$TABLES --ignore-table=$table " 43 | done 44 | else 45 | TABLES=${DBTABLES[$KEY]} 46 | fi 47 | fi 48 | 49 | for database in $DBS; do 50 | echo -n "Backing up database $database..." 51 | test ${DBHOST[$KEY]} = "localhost" && SERVER=`hostname -f` || SERVER=${DBHOST[$KEY]} 52 | mysqldump --host ${DBHOST[$KEY]} --port ${DBPORT[$KEY]} --user=${DBUSER[$KEY]} --password=${DBPASS[$KEY]} \ 53 | ${DBOPTIONS[$KEY]} $database $TABLES > $BACKDIR/$SERVER-$database-$DATE-mysqlbackup.sql 54 | $COMPRESSION_COMMAND $BACKDIR/$SERVER-$database-$DATE-mysqlbackup.sql 55 | echo "done!" 56 | done 57 | done 58 | 59 | # if you have the mail program 'mutt' installed on 60 | # your server, this script will have mutt attach the backup 61 | # and send it to the email addresses in $EMAILS 62 | 63 | if [ $MAIL = "y" ]; then 64 | BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net. \n\n" 65 | BODY=$BODY`cd $BACKDIR; for file in *$DATE-mysqlbackup.sql.$COMPRESSION_EXTENSION; do md5sum ${file}; done` 66 | ATTACH=`for file in $BACKDIR/*$DATE-mysqlbackup.sql.$COMPRESSION_EXTENSION; do echo -n "-a ${file} "; done` 67 | 68 | echo -e "$BODY" | mutt -s "$SUBJECT" $ATTACH -- $EMAILS 69 | if [[ $? -ne 0 ]]; then 70 | echo -e "ERROR: Your backup could not be emailed to you! \n"; 71 | else 72 | echo -e "Your backup has been emailed to you! \n" 73 | fi 74 | fi 75 | 76 | if [ $DELETE = "y" ]; then 77 | OLDDBS=`cd $BACKDIR; find . -name "*-mysqlbackup.sql.$COMPRESSION_EXTENSION" -mtime +$DAYS` 78 | REMOVE=`for file in $OLDDBS; do echo -n -e "delete ${file}\n"; done` # will be used in FTP 79 | 80 | cd $BACKDIR; for file in $OLDDBS; do rm -v ${file}; done 81 | if [ $DAYS = "1" ]; then 82 | echo "Yesterday's backup has been deleted." 83 | else 84 | echo "The backups from $DAYS days ago and earlier have been deleted." 85 | fi 86 | fi 87 | 88 | if [ $DUPLICITY = "y" ]; then 89 | duplicity full --progress $DUPLICITY_OPTIONS $BACKDIR $DUPLICITY_TARGET_URL 90 | fi 91 | 92 | if [ $S3_UPLOAD = "y" ]; then 93 | aws $AWS_CLI_OPTIONS s3 sync $BACKDIR s3://$S3_PATH $S3_OPTIONS 94 | fi 95 | 96 | if [ $FTP = "y" ]; then 97 | echo "Initiating FTP connection..." 98 | 99 | cd $BACKDIR 100 | ATTACH=`for file in *$DATE-mysqlbackup.sql.$COMPRESSION_EXTENSION; do echo -n -e "put ${file}\n"; done` 101 | 102 | for KEY in "${!FTPHOST[@]}"; do 103 | echo -e "\nConnecting to ${FTPHOST[$KEY]} with user ${FTPUSER[$KEY]}..." 104 | ftp -nvp <