Automatic backup of MySQL two-segment scripts in Linxu every day

Source: Internet
Author: User

Timed execution Script:


Perform

The code is as follows Copy Code

CRONTAB-E * * */bin/bash yourpath/mysqlbak.sh


Open an automatic execution file

The code is as follows Copy Code
Vi/etc/crontab

Add the following in the ETC to automate the task.

The code is as follows Copy Code

* * * * root/mysqlbak.sh

Above two 00 00 * * * for the wee hours of the day automatically execute the script

Time-sharing and Lunar Week command

M: Minutes (0-59). Per minute with * or */1
H: Hours (0-23). (0 indicates 0 points)
D: Days (1-31).
M: Month (1-12).
D: Days of the week (0~6,0 for Sunday).

Execute */5 * * * * every five minutes
0 per hour * * * * * *
Daily Execution 0 0 * * *
Perform 0 0 * * 0 per week
0 0 1 * * * per month
Annual Implementation 0 0 1 1 *


Restart Cron

The code is as follows Copy Code

/etc/rc.d/init.d/crond restart

Or

Service Crond Restart


Instance One

mysqlback.sh

The code is as follows Copy Code
#!/bin/bash
#功能说明: This feature is used to back up the database
#编写日期: 2010/12/06

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin
Export PATH
#数据库用户名
Dbuser= ' Root '
#数据库密码
Dbpasswd= ' 123456 '
#数据库名, you can define multiple databases, separated by spaces in the middle, such as: Test Test1 Test2
Dbname= ' Test1 test2 '
#备份时间
Backtime= ' Date +%y%m%d%h%m%s '
#日志备份路径
Logpath= '/second/backup '
#数据备份路径
Datapath= '/second/backup '
#日志记录头部
Echo ' "Backup Time is ${backtime}, backup database table ${dbname} start" >> ${logpath}/mysqllog.log
#正式备份数据库
for table in $dbname; Todo
Source= ' Mysqldump-u ${dbuser}-p${dbpasswd} ${table}> ${logpath}/${backtime}.sql ' 2>> ${logpath}/ Mysqllog.log;
#备份成功以下操作
If ["$?" = 0];then
CD $datapath
#为节约硬盘空间, compressing the database
Tar jcf ${table}${backtime}.tar.bz2 ${backtime}.sql >/dev/null
#删除原始文件, leaving only compressed files
Rm-f ${datapath}/${backtime}.sql
echo "database table ${dbname} backup succeeded!!" >> ${logpath}/mysqllog.log
Else
#备份失败则进行以下操作
echo "database table ${dbname} backup failed!!" >> ${logpath}/mysqllog.log
Fi
Done


Instance 2

Run every morning, automatically back up this script executes at most once a day, and only the last five days of backups are kept on the server.

The code is as follows Copy Code

#!/bin/bash
#This is a shellscript for Auto DB Backup
#Powered by Aspbiz
#2004-09

#Setting
#设置数据库名, database login name, password, backup path, log path, data file location,
#以及备份方式
#默认情况下备份方式是tar, it could be mysqldump,mysqldotcopy.
#默认情况下, log in to the MySQL database with root (empty), back up to/root/dbxxxxx.tgz
Dbname=mysql
Dbuser=root
Dbpasswd=
backuppath=/root/
Logfile=/root/db.log
dbpath=/var/lib/mysql/
#BackupMethod =mysqldump
#BackupMethod =mysqlhotcopy
#BackupMethod =tar
#Setting End


Newfile= "$BackupPath" db$ (date +%y%m%d). tgz
Dumpfile= "$BackupPath" db$ (date +%y%m%d)
Oldfile= "$BackupPath" db$ (date +%y%m%d--date= ' 5 days ago '). tgz

echo "-------------------------------------------" >> $LogFile
echo $ (date + "%y-%m-%d%h:%m:%s") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
If [f $OldFile]
Then
Rm-f $OldFile >> $LogFile 2>&1
echo "[$OldFile]delete old File success!" >> $LogFile
Else
echo "[$OldFile]no old Backup file!" >> $LogFile
Fi

If [f $NewFile]
Then
echo [$NewFile]the Backup File is Exists,can ' t backup! ' >> $LogFile
Else
Case $BackupMethod in
mysqldump)
If [-Z $DBPasswd]
Then
Mysqldump-u $DBUser--opt $DBName > $DumpFile
Else
Mysqldump-u $DBUser-p$dbpasswd--opt $DBName > $DumpFile
Fi
Tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]backup success!" >> $LogFile
RM-RF $DumpFile
;;
Mysqlhotcopy)
RM-RF $DumpFile
mkdir $DumpFile
If [-Z $DBPasswd]
Then
Mysqlhotcopy-u $DBUser $DBName $DumpFile >> $LogFile 2>&1
Else
Mysqlhotcopy-u $DBUser-P $DBPasswd $DBName $DumpFile >> $LogFile 2>&1
Fi
Tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]backup success!" >> $LogFile
RM-RF $DumpFile
;;
*)
/etc/init.d/mysqld Stop >/dev/null 2>&1
Tar czvf $NewFile $DBPath $dbname >> $LogFile 2>&1
/etc/init.d/mysqld Start >/dev/null 2>&1
echo "[$NewFile]backup success!" >> $LogFile
;;
Esac
Fi

echo "-------------------------------------------" >> $LogFile

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.