1. Requirements:Database backup is particularly important for production environments. Database backup is divided into physical backup and logical backup. Physical backup: Use related copy commands to directly copy the data in the database's data directory or make multiple copies. Commonly used tools: XtraBackup. Logical backup: Use the mysqldump command that comes with MySQL to store the data to be backed up in the form of SQL statements. Next we will use the mysqldump command to back up data. Use automated tasks for daily backups. 2. Execution file writing:1. Create a folder, and the executed sh file and database backup file are stored in this folder mkdir /usr/local/mysqlDataBackup cd /usr/local/mysqlDataBackup 2. Create a sh file and write a shell script:
Single database script: #!/bin/bash #This scripts is for auto backup databases #create by weijb at 2021-07-28 #delete data from 7 day before DATAdelete='date +%F -d "-7 day"' rm -rf /usr/local/mysqlDataBackup/*_${DATAdelete}.sql.gz MYSQL_CMD=/usr/bin/mysqldump MYSQL_USER=account MYSQL_PWD=password DATA='date +%F' DBname=test_db ${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PWD} --compact -B ${DBname} | gzip >/usr/local/mysqlDataBackup/${DBname}_${DATA}.sql.gz Analysis: (1) Delete backup data from 7 days ago to save space: rm -rf /usr/local/mysqlDataBackup/*_${DATAdelete}.sql.gz Delete the backup data of the previous 7 days to save space and only keep the latest 7 days of data (2) Define variables: Multiple database backup: #!/bin/bash #This scripts is for auto backup databases #create by weijb at 2021-07-28 #delete data from 7 day before DATAdelete='date +%F -d "-7 day"' rm -rf /usr/local/mysqlDataBackup/*_${DATAdelete}.sql.gz MYSQL_CMD=/usr/bin/mysqldump MYSQL_USER=account MYSQL_PWD=password DATA='date +%F' DBname='mysql -u${MYSQL_USER} -p${MYSQL_PWD} -e "show databases;" | sed '1,5d'' for DBname in ${DBname} do ${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PWD} --compact -B ${DBname} | gzip >/usr/local/mysqlDataBackup/${DBname}_${DATA}.sql.gz done Analysis: (1) Intercept the database that needs to be backed up
(2) Other details are the same as single database backup 3. Verify that the script is written successfully: (1) Script file authorization:
(2) Execute the script:
The files generated under /usr/local/mysqlDataBackup indicate that the writing is successful. 4. Write automatic tasks: Back up the database at 1 am every day:
Type i to edit:
Use esc to exit the file editing and enter :wq to save and exit. Check whether the task is set successfully:
Restart the crontab service:
At this point, the mysql automatic task script to back up the database is completed. This is the end of this article about using mysqldump to perform daily automatic backup of MySQL database in CentOS 7. For more relevant MySQL automatic backup using mysqldump, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to enable TLS and CA authentication in Docker
>>: The 6 Most Effective Ways to Write HTML and CSS
1. What is a virtual host? Virtual hosts use spec...
#include <linux/moduleparam.h> 1. Module pa...
Table of contents Docker image download Start mys...
Table of contents 1. Global Guard 1.1 Global fron...
1. Check whether port 80 is occupied. Generally, ...
I knew before that to synchronously obtain the re...
We are in an era of rapid development of mobile In...
Apache Tika is a library for file type detection ...
<br />"There are no ugly women in the w...
Go to https://dev.mysql.com/downloads/mysql/ to d...
In the previous article, it was mentioned that th...
This article describes how to build a phalcon env...
Note 1: The entire background in the above pictur...
Table of contents redo log Why do we need to upda...
The following code is in my test.html. The video c...