MySQL Backup shell script code _linux shell in Linux

Source: Internet
Author: User
Tags mysql backup

First step: Define your backup directory on your Linux server:

Copy Code code as follows:

Mkdir/var/lib/mysqlbackup
Cd/var/lib/mysqlbackup

The second step: The following is the most important step, is to write a scheduled backup script.

Copy Code code as follows:

VI dbbackup.sh

The code file is as follows

Copy Code code 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
gzdumpfile= $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 the days"

Friendly tip: user,name and DB Please change according to your situation

Save the exit, and then give the file the executable permission:

Copy Code code as follows:

#chmod 777 mysqlautobackup.sh

Perform backup script code with crontab:

Crontab-e

If you back up 3 o'clock in the afternoon 20 daily, add the following code,

Code:

Copy Code code as follows:
* * */var/lib/mysqlbackup/dbbackup.sh

This is done, every day will regularly help you back up the MySQL database.

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.