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 Script content: #!/bin/bash export 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.sql find $DATA_DIR/ywghblog -type f -name "*.sql" -mtime +7 -exec rm -rf {} \; Script explanation: First, define a variable NOW to get the current year, month and day. In defining a directory variable DATA_DIR, Then use the mysqldump command to back up the database to the specified directory. Finally, use the find command to find the sql files in the specified directory and delete the files older than 7 days. -mtime + is to find files outside of 7 days -mtime – find files within 7 days -exec is to execute the following command Please modify the script according to your needs. Knowledge point extension: Automatic backup of MYSQL database script vi dbbackup.sh In the opened editor, enter: #!/bin/bash /usr/local/mysql/bin/mysqldump -uuser -ppasswd databasename > /home/wwwroot/backup/date_$(date '+%Y%m%d').sql The command means to use Modify permissions and execute the backup script to see if the backup is successful: chmod +x dbbackup.sh sh dbbackup.sh If everything checks out fine, create a scheduled task to automatically back up your data. Enter the command: Enter the scheduled task in the opened file: Then press the esc key and enter :wq to exit editing and save the file. OK, the scheduled task has been created. At 23:56 every day, execute dbbackup.sh to back up the database. Summarize The above is the MySQL backup script introduced by the editor and it is retained for 7 days. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Specific use of Linux man command
>>: How to use Linux whatis command
error message: ERROR 2002 (HY000): Can't conn...
Superset is a lightweight self-service BI framewo...
Anchor tag usage: Linking to a specific location i...
Wildcard categories: %Percent wildcard: indicates...
Table of contents background Solution New Questio...
1. Create a test table CREATE TABLE `mysql_genara...
Table of contents Preface 1. Parent component pas...
Tips for using Docker 1. Clean up all stopped doc...
Preview: Code: Page Sections: <template> &l...
Before talking about CSS priority, we need to und...
This article example shares the specific code of ...
Preface There is a misunderstanding about the max...
Demand background A statistical interface, the fr...
1. Introduction to Docker Docker is developed in ...
Classification of color properties Any color can ...