1, write the shell script used by the backup database
The code is as follows |
Copy Code |
vim/home/mysqlback.sh #!/bin/bash Mysql_user=root Mysql_pass=root Database=test File_name= $DATABASE. ' Date +%y%m%d '. tar.gz Tmp_filename= $DATABASE. ' Date +%y%m%d%h%m%s '. sql.gz #切换到文件存放的目录 to prevent a warning message from being reported in tar: tar:removing leading '/' from member names cd/alidata1/mysqlback/ #删除7天以前的备份文件 RM-RF $DATABASE. $ (date-d -1day + "%y%m%d"). tar.gz Mysqldump-u$mysql_user-p$mysql_pass $DATABASE--socket=/tmp/mysql.sock | Gzip-9-> $TMP _filename Tar-czf $FILE _name $TMP _filename RM-RF $TMP _filename |
2. Use crontab to perform scheduled tasks
The code is as follows |
Copy Code |
Crontab-e 1 * * * root/home/mysqlback.sh |
Scheduled to execute every 1:00 script
Simple note
Mysql_user=root Database user Name
Mysql_pass=root Database Password
Database=test Backup Database
File_name= $DATABASE. ' Date +%y%m%d '. tar.gz generate backup files by date