当前位置:Gxlcms > 数据库问题 > linux mysql定时备份

linux mysql定时备份

时间:2021-07-01 10:21:17 帮助过:5人阅读

#!/bin/bash 2 mysqldump -uusername -ppassword -hmysqlIp DatabaseName > /dev/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql

对备份进行压缩

mysqldump -uusername -ppassword -hmysqlIp DatabaseName | gzip > /dev/backup/DatabaseName_$(date +%Y%m%d_%H%M%S).sql.gz

注意:
把 username 替换为实际的用户名;

把 password 替换为实际的密码;

把mysqlIp替换mysql的IP;

把 DatabaseName 替换为实际的数据库名;

4.添加可执行权限

chmod u+x bkDatabaseName.sh

添加可执行权限之后先执行一下,看看脚本有没有错误,能不能正常使用;

./bkDatabaseName.sh

5.添加定时执行任务

1 # crontab
2 -bash: crontab: command not found

执行 crontab 命令如果报 command not found,就表明没有安装;

安装crontab请参考:http://www.cnblogs.com/dxy1451713982/p/8081569.html

添加定时任务

crontab -e
*/1 * * * * /home/backup/bkDatabaseName.sh

表示每分钟执行一次,注:对于cron表达式,建议自己百度

6.测试任务是否执行

 1 # tail -f /var/log/cron
 2 Dec 21 17:33:10 iZ2318jid47Z run-parts(/etc/cron.daily)[22449]: finished makewhatis.cron
 3 Dec 21 17:33:10 iZ2318jid47Z run-parts(/etc/cron.daily)[15753]: starting mlocate.cron
 4 Dec 21 17:33:10 iZ2318jid47Z run-parts(/etc/cron.daily)[22460]: finished mlocate.cron
 5 Dec 21 17:33:10 iZ2318jid47Z run-parts(/etc/cron.daily)[15753]: starting prelink
 6 Dec 21 17:33:27 iZ2318jid47Z run-parts(/etc/cron.daily)[23799]: finished prelink
 7 Dec 21 17:33:27 iZ2318jid47Z run-parts(/etc/cron.daily)[15753]: starting readahead.cron
 8 Dec 21 17:33:27 iZ2318jid47Z run-parts(/etc/cron.daily)[23811]: finished readahead.cron
 9 Dec 21 17:33:27 iZ2318jid47Z run-parts(/etc/cron.daily)[15753]: starting tmpwatch
10 Dec 21 17:33:27 iZ2318jid47Z run-parts(/etc/cron.daily)[23849]: finished tmpwatch
11 Dec 21 17:33:27 iZ2318jid47Z anacron[15719]: Job `cron.daily terminated

或者去你存放备份文件的目录下查看,是否有备份文件生成;

 

 

 

linux mysql定时备份

标签:空间   情况   linux c   style   tmp   tail   read   password   df -h   

人气教程排行