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
This article mainly introduces the example analys...
The difference between http and https is For some...
Preface Nginx is a lightweight HTTP server that u...
1. Install a virtual machine (physical machine) Y...
I started learning MySQL recently. The installati...
0. System requirements CPU I5-10400F or above Mem...
Table of contents The basic principles of Vue'...
Table of contents Why do we need a material libra...
Table of contents 1. Declare a function 2. Callin...
Install ssh tool 1. Open the terminal and type th...
If you use CSS don't forget to write DOCTYPE, ...
This article shares with you the graphic tutorial...
Demand scenario: The boss asked me to use the cra...
1. Add users and groups 1. Add mysql user group #...
Preparation 1. Check whether the GPU supports CUD...