在某一文件夹下创建shell文件:
vi /var/spool/cron/mysqlbak.sh
复制如下内容:
#!/bin/bash # Name:name.sh backupdir=/www/mysqlbak time=` date +%Y%m%d%H ` /usr/bin/mysqldump dbname | gzip > $backupdir/dbname_$time.sql.gz find $backupdir -name "dbname_*.sql.gz" -type f -mtime +7 -exec rm {} ; > /dev/null 2>&1
backupdir:备份目录; dbname :你要备份的数据库名字; -mtime +7:7天删除一次;
为脚本添加执行权限
chmod +x /var/spool/cron/mysqlbak.sh
然后试运行:/var/spool/cron/mysqlbak.sh
测试可以之后,将/var/spool/cron/mysqlbak.sh加入到crontb任务就行。