MySQL 备份 InnoDB

每天备份策略

cd /etc/cron.daily/

touch /etc/cron.daily/dbbackup-daily.sh

chmod 755 /etc/cron.daily/dbbackup-daily.sh

vi /etc/cron.daily/dbbackup-daily.sh

然后写入以下内容


#!/bin/bash

dbs=( "db1" "db2" "db3" )

for db in "${dbs[@]}"
do
        now="$(date +'%Y_%m_%d_%H_%M_%S')"
        filename="${db}_backup_$now".gz
        backupfolder="/home/yang/dbbackup"
        fullpathbackupfile="$backupfolder/$filename"
        logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
        echo "mysqldump ${db} started at $(date +'%Y_%m_%d %H:%M:%S')" >> "$logfile"

        /home/uniweibo/mysql/mysql-5.6.17/bin/mysqldump -h127.0.0.1 --single-transaction -uroot -P6606 -pxxxxx ${db} | gzip > "$fullpathbackupfile"

        echo "mysqldump ${db} finished at $(date +'%Y_%m_%d %H:%M:%S')" >> "$logfile"

        #chown myuser "$fullpathbackupfile"
        #chown myuser "$logfile"
        #echo "file permission changed" >> "$logfile"

        find "$backupfolder" -name ${db}_backup_* -mtime +8 -exec rm {} \;
        echo "old files deleted" >> "$logfile"
        echo "operation finished at $(date +'%Y_%m_%d %H:%M:%S')" >> "$logfile"
        echo "*****************" >> "$logfile"
done

参考资料:

我在此基础之上, 小作了下修改.