The first is to find a tutorial online, the code is very simple
#!/bin/bashfolder =~/test time = ' date +%y%m% d ' mysqldump -u user-p pwd -hlocalhost databasename | gzip > $folder/databasename_$time Span style= "COLOR: #000000" >.sql.gz# find $folder-name " databasename_*.sql.gz -type f-mtime +7 -exec rm {} \;# gunzip < $folder/databasename_$time . sql.gz | mysql-hlocalhost-uuser-ppwd DatabaseName
Because writing Python code is accustomed to, encounter = will naturally knock the space, the result many variables have no value, so here need to notice, then user and PWD is your own data username and password, databasename is the name of the database you want to manipulate, Because you do not want to waste memory directly mysqldump generated SQL file compression processing, and then the middle of the command is to find the seven days before the file and delete operations,-exec is the value of the command will be executed, curly braces are executed object collection, followed by a semicolon end, Add a backslash to prevent escaping, the third line is to restore data from the gzip file to the database, and at first I was using gzip and then reported an error:
Error:ascii ' unless ' appeared in the statement, it is not allowed, option--binary-mode are enabled and MySQL is R UN in non-interactive mode. Set--binary-mode to 1 if ASCII '/' is expected. Query: '.
I went to search for it. There is no good solution, and then the SQL statement extracted with the command found can execute. sql file, and then try not to use gzip instead of using Gunzip, then the script will run successfully and there is no similar error.
The timing of the subsequent execution is incidentally mentioned,
chmod +x bashname. SH --Add Execute permission for script
# crontab-e3 * * * * root ~/bashname. SH # indicates that the script executes 3:00 every day
Then restart the crontab.
Linux backs up MySQL files and recovers scripts, and errors that occur in them: Error:ascii ' appeared in the statement