centos6自动备份mysql并删除旧文件

一、准备工作

由于在本地备份不需要上传,暂时只需要用到crontab定时任务

yum install vixie-cron

yum install crontabs              //安装

chkconfig crond on                //开机自启动
service crond start                 //启动

 

二、编写shell

[root@localhost bf]# vi bf.sh 
#!/bin/bash
# Name:beifenmysql.R
# This is a ShellScript For Auto mysql Backup and Delete old Backup

backupdir=/bf    //备份文件存放路劲
time=` date +%Y%m%d%H%M%S `    //注意 ` 必须是tab键上面的符号,日期显示到秒
mysqldump -u root -p123456 数据库名 > $backupdir/hdt_$time.sql    //利用mysqldump工具备份
find $backupdir -name "hdt_*.sql" -type f -mmin +6 -delete > /dev/null 2>&1

//很多说法利用-exec  rm  { }  \  ;删除,但是我没有效果,将文件查找出来放入回收站

//解决上述遗留问题: \ 和 ; 之间不能有空格

 

三、赋予权限并放入定时任务

[root@localhost bf]# chmod 755 bf.sh 
[root@localhost bf]# crontab -e

*/2 * * * * /bf/bf.sh
~
表示每隔两分钟执行一次

service crond restart                 //修改配置文件需要重新启动

 

四、执行效果

[root@localhost bf]# ll
总用量 24
-rwxr-xr-x. 1 root root  289 8月   4 17:04 bf.sh
-rw-r--r--. 1 root root 1263 8月   4 17:16 hdt_20180804171601.sql
-rw-r--r--. 1 root root 1263 8月   4 17:18 hdt_20180804171801.sql
-rw-r--r--. 1 root root 1263 8月   4 17:20 hdt_20180804172001.sql
drwxr-xr-x. 2 root root 4096 8月   4 16:45 sql
[root@localhost bf]# ll
总用量 24
-rwxr-xr-x. 1 root root  289 8月   4 17:04 bf.sh
-rw-r--r--. 1 root root 1263 8月   4 17:18 hdt_20180804171801.sql
-rw-r--r--. 1 root root 1263 8月   4 17:20 hdt_20180804172001.sql
-rw-r--r--. 1 root root 1263 8月   4 17:22 hdt_20180804172201.sql
drwxr-xr-x. 2 root root 4096 8月   4 16:45 sql

扫描二维码关注公众号,回复: 2736954 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_39855998/article/details/81427717