[Go] A simple shell script that backs up a MySQL database

Source: Internet
Author: User

This article is translated from Isystemadmin, "a simple Shell Script to Backup MySQL Database"

Shell scripts are scripts that let us write different types of commands that can be executed in this file. We can also do this manually by typing the command individually. If we were to use a shell script, we had to write these commands to a text file at the beginning, and then we could run the commands again and again at will.

The first thing I want to bring to you in this article is the complete script. The script will be described later. I assume you already know shell scripting, mysqldump and crontab.

Applicable operating system: any Linux or UNIX.

Main script (for backing up MySQL database):

The shell script can automatically back up the database. Just copy and paste this script into a text editor and enter the database user name, password, and database name. I backed up the database using the mysqlump command. Each line of script commands is described later.

1. Set up directory "Backup" and "Oldbackup" respectively

# Mkdir/backup # Mkdir/oldbackup

2. Now use your favorite editing software to create and edit "backup.sh"

I'm using VI here.

# vi/backup/backup.sh

Now enter the following lines of commands into the backup.sh file:

# !bin/bashCD/ in Backup Directory "mv Backup*/Oldbackupecho" Old Databases is Moved to Oldbackup folder "now =$ (date +"%d-%m-%y--%h:%m:%S ") File=backup-$Now. Sqlmysqldump–u user-name  $Fileecho "Your Database Backup successfully completed"

Script Description:

Remember, in line 8th, you enter your database user name, password, and database name after the mysqldump command.

Executes the script, first enters the /backup directory, then the script will move the old database backup to the /oldbackup folder, and then generate a file name based on the date and time of the system, at the end of the mysqldump command generates a database backup file in the format ". sql".

3. Set the executable license for the backup.sh script file

# chmod +x/backup/backup.sh

4. Execute the Script

# ./backup.sh

The following input is given when the script finishes running.

[Email protected]:/download#/backup.shyouareindownload directoryold backup databaseismoved to Oldbackup folderdatabase backup successful completedroot@Server1:/download#

Note: The first execution of the script will have a "No such file" prompt, because the old backup files do not yet exist. As long as the script executes again there is no problem, the problem is no longer there.

5. Use cron to make a backup plan

The script can be executed periodically using cron, and the backup will be done automatically. Use the crontab command to edit the scheduled tasks performed by Cron.

# crontab–e

Just add the following line of code to the editor to save it.

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. More information about Cron task settings can be found in the Crontab manual.

This is a very basic script for beginners. Hopefully you can extrapolate write more complex backup scripts. We will try to provide new scripts that are more automated. Please advise us, we will try our best to solve your problem. Thanks to accompany us.

[Go] A simple shell script that backs up a MySQL 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.