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

An elegant way to handle WeChat applet authorization login

Preface When the WeChat mini program project invo...

js object-oriented method to achieve drag effect

This article shares the specific code for impleme...

Four ways to modify the default CSS style of element-ui components in Vue

Table of contents Preface 1. Use global unified o...

Detailed explanation of VUE Token's invalidation process

Table of contents Target Thought Analysis Code la...

MySQL trigger usage scenarios and method examples

trigger: Trigger usage scenarios and correspondin...

Simple tutorial on using Navicat For MySQL

recommend: Navicat for MySQL 15 Registration and ...

javascript:void(0) meaning and usage examples

Introduction to void keyword First of all, the vo...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...

How to find and delete duplicate rows in MySQL

Table of contents 1. How to find duplicate rows 2...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

Analysis of the difference between Mysql InnoDB and MyISAM

MySQL supports many types of tables (i.e. storage...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

Detailed explanation of ActiveMQ deployment method in Linux environment

This article describes the deployment method of A...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...