Home >Database >Mysql Tutorial >MySQL rolling backup techniques for data

MySQL rolling backup techniques for data

WBOY
WBOYOriginal
2023-06-15 19:47:441522browse

MySQL is a popular relational database that is widely used in various fields. However, like other applications, MySQL has risks such as data corruption, crashes, and malicious attacks. Therefore, backing up your data is crucial.

Backups can provide security and some form of "undo" functionality to data, reducing or even eliminating instability and risk. The most common backup types are full backup and incremental backup. However, if you need frequent, real-time backups, rolling backups are a better approach.

Rolling backup refers to automatically backing up all data within acceptable time intervals. These time intervals are called backup intervals. A rolling backup deletes the oldest backup and creates a new backup whenever a backup is performed. The biggest advantage of this method is that it can ensure real-time performance and data recoverability.

The following are tips on how to use MySQL to implement rolling backup of data:

  1. Create a backup script

Create a backup script that can execute the mysqldump command to back up the database. Additionally, you need to specify the name and location of the backup file, as well as the backup interval. The following is an example of a backup script:

#!/bin/bash 

# Set database credentials 
user="username" 
password="password" 
host="localhost" 
db_name="database_name" 

# Set backup directory and filename 
backup_dir="/backup/mysql" 
timestamp=$(date +%Y%m%d-%H%M%S) 
backup_name="$db_name-$timestamp.sql.gz" 

# Remove old backups 
find "$backup_dir" -type f -mtime +15 -delete 

# Create backup 
mysqldump --user=$user --password=$password --host=$host $db_name | gzip > "$backup_dir/$backup_name" 

echo "Backup created successfully: $backup_name"

In the above backup script, you need to replace user, password, host and db_name with your own MySQL credentials and database name. In addition, the backup script will delete the backup 15 days ago at each backup and save the new backup to the specified backup_dir directory.

  1. Create a scheduled task

Next, you need to set the backup script as a scheduled task to ensure that the backup operation is performed at a certain time. In Linux you can use cron job scheduler. Enter the following command at the command line to edit the cron job:

crontab -e

Then, add the following lines to the cron job file to perform backups daily and leave 2 hours between each backup:

0 */2 * * * /bin/bash /path/to/backup_script.sh

In the above line, /bin/bash /path/to/backup_script.sh is the path to the backup script. This means that the backup script will be executed every two hours.

  1. Restore database

If you need to restore data, just use the following command:

gunzip < backup.sql.gz | mysql -u [user] -p[password] [database_name]

The restore process will use gzip to decompress the backup file and Data is loaded into the specified MySQL database.

In short, using MySQL to implement data rolling backup can ensure that you can maintain data integrity and recoverability even in unexpected circumstances. The above steps provide a basic framework so that real-time backups can be automated.

The above is the detailed content of MySQL rolling backup techniques for data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn