Linux regularly backs up the MySQL database shell script

Source: Internet
Author: User
Tags mkdir

The shell script is a script that we write different types of commands that can be executed in this file. We can also

A typing command is performed manually. If we're going to use a shell script, we have to write these commands in a text file at the beginning, and then

You can run these commands over and over again.
Suppose you already know shell scripting, mysqldump and crontab. First give the complete script, which will be followed by the foot

This description.
Applicable operating system: any Linux or UNIX.

1. Create a script
The shell script can automatically back up the database. Just copy and paste this script into the text editor, enter the database username, secret

Code and database name. Here we back up the database using the mysqldump command. Each line of script commands is described later


(1) Create two directories "Backup" and "Oldbackup" in the directory where you want to place the backup files, using the root directory

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 of the backup.sh script file

The code is as follows Copy Code
1 # chmod +x/backup/backup.sh

(4) Script execution


1 #./backup.sh
The following output is available when the script is run:

The code is as follows Copy Code


1 root@server1:/download#./backup.sh

2 Areindownload Directory

3 Old Backup databaseismoved to Oldbackup folder

4 Database backup successful completed

5 root@server1:/download#

Note: The first execution of the script will have a "No such file" hint, because the old backup file does not exist yet. Just

There is no problem with executing the script again, and the problem is no longer there.

2. Script description
In line 8th, enter your own database user name, password, and database name after the mysqldump command.
Execute the script, first go into the/backup directory (to be consistent with the directory you created), and then the script will take the original

Old database backup is moved to the/oldbackup folder, then a filename is generated based on the system's date and time, and at the end

The mysqldump command generates a database backup file in the. sql format.

3. Use cron to develop a backup plan
Using cron, you can execute the script regularly and the backup will be done automatically. Edit Cron-Executed scheduled tasks using the crontab command

The code is as follows Copy Code

1 #crontab –e

Enter the code in the editor and save the exit:

The code is as follows Copy Code

1 013* * * */backup/backup.sh

This task indicates that the database is backed up to the specified folder at 1 o'clock in the afternoon every day (the specific backup cycle can be based on its own actual

Situation set). Detailed information about cron task settings can be found in the Crontab manual.
Here a regular automatic backup MySQL function can be realized, in order to ensure the security of the database, Lee bad advice we all develop

The habit of backing up your database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.