linux下mysql定时备份命令

linux下,如需对mysql做备份,可以做个shell文件,一劳永逸

[root@iZ2zeavn7cbxg2g9bs5donZ mysqlbackup]# cat mysql_backup.sh 
#!/bin/sh

. /etc/profile
. ~/.bash_profile
db_user='root'
db_password='123456'
db_name='openhis'
backup_dir='/usr/local/mysqlbackup/'
current_time=$(date +'%Y-%m-%d_%H%M%S')
filepath=$backup_dir$current_time'.sql.gz'
echo '开始导出数据库...'
mysqldump -u$db_user -p$db_password $db_name | gzip > $filepath
echo '导出成功,文件名为: '$filepath

效果:

[root@iZ2zeavn7cbxg2g9bs5donZ mysqlbackup]# sh mysql_backup.sh 
开始导出数据库...
导出成功,文件名为: /usr/local/mysqlbackup/2021-07-28_113424.sql.gz
[root@iZ2zeavn7cbxg2g9bs5donZ mysqlbackup]# ll
total 5416
-rw-r--r-- 1 root root 881615 Jul 21 10:19 2021-07-21_101944.sql.gz
-rw-r--r-- 1 root root 881635 Jul 21 10:21 2021-07-21_102115.sql.gz
-rw-r--r-- 1 root root 881691 Jul 21 10:33 2021-07-21_103305.sql.gz
-rw-r--r-- 1 root root 955447 Jul 27 18:27 2021-07-27_182711.sql.gz
-rw-r--r-- 1 root root 961723 Jul 28 09:22 2021-07-28_092235.sql.gz
-rw-r--r-- 1 root root 963172 Jul 28 11:34 2021-07-28_113424.sql.gz
-rwxrwxrwx 1 root root    488 Jul 28 11:34 mysql_backup.sh

如果需要定时自动备份,可以使用linux的 crontab命令

[root@iZ2zeavn7cbxg2g9bs5donZ mysqlbackup]# crontab -e

0 0 10 * * ? * . /etc/profile;/bin/sh /home/lanpu/usr/local/mysqlbackup/mysql_backup.sh

0 0 10 * * ? * //每天上午10点运行
后面跟.sh文件路径

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值