Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql backup script and keep it for 7 days

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 273

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report