定时自动备份mysql数据库

CopyTo:https://www.cnblogs.com/foolishnoob/p/5421462.html

新建备份文件并赋予可以执行的权限

mkdir -p /home/mysql_backup/
touch /home/mysql_backup/mysql_backup.sh
chmod 551 /home/mysql_backup/mysql_backup.sh

编辑/home/mysql_backup/mysql_backup.sh

vim /home/mysql_backup/mysql_backup.sh

写入以下内容

backupdir=/home/mysql_backup
time=` date +%Y_%m_%d_%H_%M_%S `
db_user=root
db_pass=123456
mysqldump --all-databases -u $db_user -p$db_pass | gzip > $backupdir/$time.sql.gz
find $backupdir -name "*.sql.gz" -type f -mtime +5 -exec rm -rf {} \; > /dev/null 2>&1

编辑crontab

crontab -e

在最后一行加入

* */1 * * * root /home/mysql_backup/mysql_backup.sh

重启crontab

service crond restart
posted @ 2018-10-24 10:29  少时不知贵  阅读(190)  评论(0编辑  收藏  举报