mysql backup script and keep it for 7 days

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

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 mysqldump to export the database named databasename to the /home/wwwroot/backup/ folder and name it date_date.sql. The one after -u is your MySQL username, the one after -p is your MySQL password, and databasename is the name of the database to be backed up. Replace these three with your own.

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:

crontab -e

Enter the scheduled task in the opened file:

56 23 * * * /root/dbbackup.sh

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!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to automatically backup the script for Linux servers (mysql, attachment backup)
  • Shell script to implement mysql scheduled backup, deletion and recovery functions
  • How to use shell scripts to automatically back up multiple MySQL databases every day
  • MySQL incremental backup and breakpoint recovery script example
  • Write a mysql data backup script using shell
  • Detailed explanation of how to use the mysql backup script mysqldump
  • Automatic backup of MySQL database using shell script
  • How to write a MySQL backup script

<<:  Specific use of Linux man command

>>:  How to use Linux whatis command

Recommend

Steps to install superset under win10 system

Superset is a lightweight self-service BI framewo...

Example sharing of anchor tag usage in HTML

Anchor tag usage: Linking to a specific location i...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Implementation of element shuttle frame performance optimization

Table of contents background Solution New Questio...

MySQL loop inserts tens of millions of data

1. Create a test table CREATE TABLE `mysql_genara...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

Detailed introduction to CSS priority knowledge

Before talking about CSS priority, we need to und...

Vue implements graphic verification code

This article example shares the specific code of ...

How to test the maximum number of TCP connections in Linux

Preface There is a misunderstanding about the max...

MySQL query data by hour, fill in 0 if there is no data

Demand background A statistical interface, the fr...

Classification of web page color properties

Classification of color properties Any color can ...