Join the Crontab timed task to perform daily. The effect is: Back up the database to the specified directory every day, delete the files backed up 10 days ago.
Here's what the script reads:
The code is as follows |
Copy Code |
#!/bin/bash Mysql_host= "127.0.0.1" Mysql_user= "Root" Mysql_passwd= "********"
Back_dir= "/www/backup/database/" ignore_db= "Information_schema|mysql|performance_schema" Db_arr= ' Mysql-u${mysql_user}-p${mysql_passwd}-h${mysql_host}-bse "show databases;" | Grep-ve "(${ignore_db})" ' datetime=$ (Date +%y-%m-%d-%h_%m_%s) For dbname in ${db_arr} Todo Mysqldump-u${mysql_user}-p${mysql_passwd}-h${mysql_host} ${dbname} | gzip > ${back_dir}${dbname}-${datetime}.sql.gz Done Find ${back_dir}-mtime +10-name "*.*"-exec rm-rf {}; |
The backup file cannot be generated correctly on the Aliyun server crontab the automatic backup database.
Check it out and see that all the settings are correct. Ah, it's strange that this script runs on my American VPS.
And, I directly execute the. sh file run script, all normal OH. Did crontab not perform at all?
So I set the crontab to execute in a minute and then add:>/web/test.log to the back 2>&1
A minute later, I looked at the generated log file and found the problem:
The code is as follows |
Copy Code |
Mysql-u${mysql_user}-p${mysql_passwd}-h${mysql_host} Mysqldump-u${mysql_user}-p${mysql_passwd}-h${mysql_host} ${dbname} |
The above two statements fragment, MySQL and mysqldump are not recognized, and then changed to:
/usr/local/mysql/bin/mysql and/usr/local/mysql/bin/mysqldump (my MySQL installation directory is/usr/local/mysql)
Problem solved, this should be the problem of environmental variables.