Mysql backup script and keep it for 7 days
Script requirements:
Back up the mysql database every day and keep the script for 7 days.
Stored in the / opt/dbbak directory.
The script name is database_xxxx-xx-xx.sql
Content of the script:
#! / bin/bashexport NOW= "$(date +"% Y-%m-%d ")" export DATA_DIR=/opt/dbbak/usr/local/ywgh/mysql/bin/mysqldump-- opt-uroot-p 'mypassword' ywghblog > $DATA_DIR/ywghblog/ywghblog_$NOW.sqlfind $DATA_DIR/ywghblog-type f-name "* .sql"-mtime + 7-exec rm-rf {}\
The script explains:
First define a variable NOW to take the current year, month and day
In defining the variable DATA_DIR of a directory
Then use the mysqldump command to back up the backed up library to the specified directory
Finally, use the find command to find the sql file in the specified directory and delete the files that are 7 days away.
-mtime + means to find files that are 7 days away
-mtime.-is to find the file within seven days.
-exec is to execute the following command.
Please modify the script according to your own needs.
Knowledge extension: automatic backup of MYSQL database scripts
Vi dbbackup.sh type in the open editor:
#! / bin/bash/usr/local/mysql/bin/mysqldump-uuser-ppasswd databasename > / home/wwwroot/backup/date_$ (date'+% Y% m% d'). Sql
The command means to use mysqldump to direct the database named databasename to the / home/wwwroot/backup/ folder and name it date_ date .sql,-u followed by your Mysql user name,-p followed by Mysql password, databasename is to back up the database name, replace these three with your own.
Modify the permissions and execute the backup script to see if the backup is successful:
Chmod + x dbbackup.shsh dbbackup.sh
If everything is all right, create a scheduled task to automatically back up your data and enter the command:
Crontab-e
Enter scheduled tasks in the open file:
56 23 * / root/dbbackup.sh
Then press the esc key and enter: wq to exit editing and save the file.
All right, the scheduled task is created, backup and perform dbbackup.sh backup of the database at 23:56 every day.
Summary
The above is the mysql backup script introduced by the editor and kept for 7 days. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!
If you think this article is helpful to you, you are welcome to reprint it, please indicate the source, thank you!