backup 定期备份 mysql database
需求: 定期备份mysql 特定的数据库
Pre:
- mysql socket 路径;
# netstat -ln | grep mysql
unix 2 [ ACC ] STREAM LISTENING 37194 /tmp/mysql.sock
- 备份命令,以dbname+日期命名:
mysqldump --socket=/tmp/mysql.sock -u root -ppassword dbname > dbname_back_`date +%F`.sql
-
mount 到公共盘,就无需scp了;
-
准备sh 文件,备份db到特定路径下,并且打包,并且删除大于30天的备份:
#!/bin/bash
backdir=/mnt/backup_db
d=`date +%F`
mysqldump --socket=/tmp/mysql.sock -u root -ppassword dbname > $backdir/dbname_back_$d.sql
cd $backdir
gzip *_$d.sql
find ./ -name "*.gz" -mtime +30 |xargs rm
- 将sh文件设置成可执行文件:
chmod +x /mnt/backup_db/mysql_backup.sh
- 确定备份频率,每周五早上7点59执行;
# crontab -e
59 07 * * 5 /bin/bash /mnt/backup_db/mysql_backup.sh >/tmp/mysql_backup.log 2>/tmp/mysql_backup.log