MySQL Automatic backup script

Source: Internet
Author: User

The first step: Define the backup directory in your Linux server:
Copy the 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 the code code as follows:

VI dbbackup.sh
The code files are as follows
Copy the 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"
Friendly tips: where user,name and DB please modify according to your own situation
Save the exit and give the file the executable permission:
Copy the code code as follows:

#chmod 777 mysqlautobackup.sh


Execute backup script code with CRONTAB timing:
Crontab-e
If you are backing up 3 o'clock in the afternoon 20 every day, add the following code
Code:
Copy the code code as follows:
* * * */var/lib/mysqlbackup/dbbackup.sh

End

MySQL Automatic backup script

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.