Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize automatic backup of MySQL Database every day in CentOS/RHEL system

Shulou Source: shulou.com Published: 2022-05-31 14:12:30 10月03日 Update

This article mainly introduces how the CentOS/RHEL system automatically backs up the MySQL database every day. It is very detailed and has a certain reference value. Interested friends must finish reading it!

1. First, create the following three folders under the server: / bak, / bak/bakmysql, / bak/bakmysqlold.

[root@server21] # mkdir-p / bak/bakmysql {, old}

Create a shell script under the / bak/bakmysql folder:

[root@server21 ~] # touch / bak/bakmysql/backup.sh

To authorize a file:

[root@server21 ~] # chmod 755 / bak/bakmysql/backup.sh

two。 Edit the shell script:

[root@server21 ~] # vim / bak/bakmysql/backup.sh

#! / bin/bash

Cd / bak/bakmysql

Echo "You are in bakmysql directory"

Mv bakmysql* / bak/bakmysqlold

Echo "Old databases are moved to bakmysqlold folder"

Now=$ (date + "% Y-%m-%d")

File=bakmysql-$Now.sql

Mysqldump-uroot-paired password 'db_bbs > $File

Echo "Your database backup successfully completed"

SevenDays=$ (date-d-7day + "% Y-%m-%d")

If [- f / bak/bakmysqlold/bakmysql-$SevenDays.sql]; then

Rm-rf / bak/bakmysqlold/bakmysql-$SevenDays.sql

Echo "You have delete 7days ago backup file"

Else

Echo "7 days ago backup file not exist"

Fi

Change the red font in the 3.Shell script mysqldump-uroot-paired password 'db_bbs > $File statement to your own mysql database user, password, and database name, respectively. The script first moves the backup files whose bakmysql starts with the file name in the / bak/bakmysql folder to / bak/bakmysqlold, then generates the latest backup files, and finally determines whether the files from seven days ago exist in the / bak/bakmysqlold folder, deletes them, and ends at last. This automatically backs up the database scripts for the last seven days.

4. Set the shell script to be executed automatically every day:

[root@server21 ~] # vim / etc/crontab add this line at the end (please refer to the notes in / etc/crontab for details)

0 0 * root / bak/bakmysql/backup.sh

Restart crontab:

[root@server21 ~] # / sbin/service crond restart

If crontab is not set to boot, you can set it to boot as follows:

[root@server21 ~] # chkconfig-level 35 crond on

The above is all the contents of the article "how to automatically back up MySQL database in CentOS/RHEL system every day". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Files scripts backups folders data databases systems content articles references lines values interests meanings fonts passwords boys buddies beginnings file names Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Linux Apple MySQL