MySQL backup shell script code in Linux

Source: Internet
Author: User
Tags mysql backup

Step 1: Define the backup directory on your Linux Server:

CopyCode The Code is as follows: mkdir/var/lib/mysqlbackup
CD/var/lib/mysqlbackup

Step 2: The following is the most important step: Write a regular backup script.

Copy code The Code is as follows: VI dbbackup. Sh

The code file is as follows:

Copy code The Code is as follows :#! /Bin/sh
# Mysql Data backup script
#
# Use mysqldump -- help, get more detail.
#
Bakdir =/root/back/ysqlbackup
Logfile =/root/back/MySQL/mysqlbak. Log
Date = 'date + % Y % m % d % H % m % s'
Echo ""> $ logfile
Echo ""> $ logfile
Echo "-----------------------------------------"> $ logfile
Echo $ (date + "% Y-% m-% d % H: % m: % s") >>$ logfile
Echo "--------------------------"> $ logfile
CD $ bakdir
Dumpfile = $ date. SQL
Gzdumpfile1_date. SQL .tar.gz
/Usr/local/bin/mysqldump-u user-pname -- database-H 127.0.0.1 dB> $ dumpfile
Echo "Dump done" >>$ logfile
Tar czvf $ gzdumpfile $ dumpfile >>$ logfile 2> & 1
Echo "[$ gzdumpfile] backup success! ">>> $ Logfile
Rm-F $ dumpfile
# Cd $ bakdir/daily
CD $ bakdir
Echo "backup done! "
Echo "Please check $ bakdir directory! "
Echo "copy it to your local disk or FTP to somewhere !!! "
Find $ bakdir-ctime + 30-exec RM {};
Echo "delete file over 30 days"

Tip: Modify the user, name, and DB according to your situation.

Save and exit, and then grant the executable permission to the file:

Copy code The Code is as follows: # chmod 777 mysqlautobackup. Sh

Run the backup script code regularly with crontab:

Crontab-e

If you back up data at three o'clock P.M. every day, add the following code,

Code:

Copy code The Code is as follows: 20 15 ***/var/lib/mysqlbackup/dbbackup. Sh

In this way, you can back up the MySQL database regularly every day.

Related Article

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.