In liunux, we first define a Shell script to back up mysql, and then execute the script periodically through Cron in linux. The backup will be completed automatically, next I will share my mysql backup process.
Shell scripts are a script for writing different types of commands. These commands can be executed in this file. We can also
Run the command manually. If we want to use shell scripts, we must write these commands to a text file at the beginning.
You can run these commands repeatedly at will.
Suppose you already know shell scripting, mysqldump, and crontab. First, the complete script is provided.
In this tutorial.
Applicable OS: Any Linux or UNIX.
1. Create a script
The Shell script can automatically back up the database. Just copy and paste the script to the text editor and enter the database user name and password
And the database name. Here we use the mysqldump command to back up the database. The following describes each line of script commands.
.
(1) create two directories "backup" and "oldbackup" under the directory where you want to place the backup file. The root directory is used.
The Code is as follows: |
Copy code |
1 # mkdir/backup
2 # mkdir/oldbackup |
(2) create and edit the file "backup. sh"
The Code is as follows: |
Copy code |
1 #! Bin/bash 2 cd/backup 3 echo "You are In Backup Directory" 4 mv backup */oldbackup 5 echo "Old Databases are Moved to oldbackup folder" 6 Now = $ (date + "% d-% m-% Y -- % H: % M: % S ") 7 File = backup-$ Now. SQL 8 mysqldump-u user-name-p 'Password' database-name> $ File 9 echo "Your Database Backup Successfully Completed" |
(3) set the executable license for the backup. sh script file
The Code is as follows: |
Copy code |
1 # chmod + x/backup. sh
|
(4) Script Execution
1 #./backup. sh
After the script is run, the following output is displayed:
The Code is as follows: |
Copy code |
1 root @ Server1:/download #./backup. sh
2 You areinDownload Directory 3 Old Backup DatabaseisMoved to oldbackup folder 4 database backup successful completed 5 root @ Server1:/download # |
Note: When the script is executed for the first time, a message "no such file" is displayed, because the old backup file does not exist. As long
If you execute the script again, the problem does not exist.
2. Script description
In the 8th-line command, enter your database username, password, and database name after mysqldump.
Run the script to enter the/backup Directory (to be consistent with the directory created by yourself), and then the script will
The old database backup is moved to the/oldbackup folder, and a file name is generated based on the system date and time.
The mysqldump command generates a database backup file in the ". SQL" format.
3. Use cron to create a backup plan
You can use Cron to regularly execute the script, and the backup will be completed automatically. Use the crontab command to edit the scheduled tasks executed by cron.
.
The Code is as follows: |
Copy code |
1 # crontab-e
|
Enter the code in the editor and save and exit:
The Code is as follows: |
Copy code |
1 013 ****/backup. sh
|
This task backs up the database to the specified folder at one o'clock P.M. every day (the specific backup cycle can be based on your actual
Situation setting ). For details about cron task settings, refer to the crontab manual.
Here, the function of regular and automatic mysql backup is implemented. To ensure database security, we recommend that you develop
The habit of backing up databases.