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
Preface When the WeChat mini program project invo...
This article shares the specific code for impleme...
There are two types of html tags, inline elements...
Table of contents Preface 1. Use global unified o...
Table of contents Target Thought Analysis Code la...
trigger: Trigger usage scenarios and correspondin...
recommend: Navicat for MySQL 15 Registration and ...
Introduction to void keyword First of all, the vo...
The command format for mysql login is: mysql -h [...
Table of contents 1. How to find duplicate rows 2...
Table of contents What utilities does VueUse have...
MySQL supports many types of tables (i.e. storage...
Everyone must know the composition of the box mod...
This article describes the deployment method of A...
border-radius: CSS3 rounded corners Syntax: borde...