#!/bin/Bashdb_user="Root"db_passwd="123456"db_name="test_db"#进入备份目录将之前的移动到old目录cd/mnt/backup/BakmysqlEcho "Bakmysql Directory now"MVtest_db*/mnt/backup/BakmysqloldEcho "Old databases is moved to Bakmysqlold folder"#备份目录backup_dir="/mnt/backup/bakmysql"#时间格式 Time=$(Date+"%y-%m-%d"#mysql Backup commands, note that there are spaces and no spaces mysqldump-U$DB_USER-P$DB_PASSWD $db _name >"$backup _dir/$db _name"-"$time. SQL"Echo "your database backup successfully completed"#这里将7天之前的备份文件删掉SevenDays=$(Date-d-7day +"%y-%m-%d")if[-f/mnt/backup/bakmysqlold/pashanhu-$SevenDays. sql] ThenRM-rf/mnt/backup/bakmysqlold/pashanhu-$SevenDays. SQLEcho "You have delete 7days ago Bak sql file"ElseEcho "7days ago Bak sql file not exist"Echo "Bash complete"fi
Then use the following command to step into the system's scheduled tasks
Crontab-e00 * * *./usr/sbin/bakmysql. SH
Save after you exit.
may need to restart crontab enable command:/sbin/service crond restart
MySQL Automatic backup under CentOS