MySQL backup script

Source: Internet
Author: User
Tags mysql backup

#! /Bin/bash
# Shell Variables
Shellname = "mysqlbak"
Scriptdir = "/data/dbbackup /"
Backupdir = "/data/dbbackup /"
Logfile = $ scriptdir "/" $ shellname ". log"

# Date/time variables Ables
Remaindays = "3"
Cdate = $ (Date "+ % Y-% m-% d ")
Ctime = $ (Date "+ % H-% m-% s ")

# Rsync Variables
Rsyncuser = "backup"
Rsyncpass = $ scriptdir "/rsyncpasswd"
Rsynccmd = "/usr/bin/rsync"
Dhost = "192.168.1.12"
Ddir = "expert"
Sdir = $ cdate

# Mysqldump tables Ables
Mysqluser = "root"
Mysqlpassed = "123"
Mysqldumpcmd = "/usr/bin/mysqldump"
Dbname = "blog"

# Write log
Writelog (){
Errinfo = $1
Echo "$ cdate $ ctime :$ {shellname }:: {errinfo}" >$ {logfile}
}

# Alters the scheduling priority of Shell
Low_priority (){
Writelog "begin renice priority of shell"
Renice + 19-p $>/dev/null 2> & 1
If [$? -Ne 0]; then
Writelog "renice failed"
Return 1
Else
Writelog "renice success"
Return 0
Fi
}

# Delete old backup data with remaindays
Delete_dir (){
Writelog "begin to delete old dir"
CD $ backupdir
Find./-maxdepth 1-name "2011 *"-type D-mtime + $ remaindays-exec Rm-RF {}\;
If [$? -Ne 0]; then
Writelog "delete old dir failed"
Return 1
Else
Writelog "delete old dir success"
Return 0
Fi
}

# Copy the backup data to backup server
Rsync_file (){
Writelog "begin to rsync file"
CD $ backupdir;
$ Rsynccmd-vzrtopg $ sdir $ rsyncuser @ $ dhost: ddir -- password-file = $ rsyncpass
If [$? -Ne 0]; then
Writelog "rsync $ sfile to $ dhost failed"
Return 1;
Else
Writelog "rsync $ sfile to $ dhost success"
Return 0;
Fi
}

Mysql_dump (){
Writelog "begin to mysqldump"
Mysqldumpoption = "-U" $ mysqluser "-P" $ mysqlpass "-- Hex-blob-r -- no-autocommit"
Filename = "$ dbname" _ "$ cdate" _ "$ ctime" ". SQL"

CD $ backupdir
$ Mysqldumpcmd $ mysqldumpoption $ dbname> $ filename
If [$? -Ne 0]; then
Writelog "mysqldump $ dbname failed"
Return 1
Else
Writelog "mysqldump $ dbname success"
Fi

Tar-zcvf $ FILENAME ". tgz" $ filename
If [$? -Ne 0]; then
Writelog "Tar-zcvf" $ FILENAME ". tgz $ filename failed"
Return 1
Else
Writelog "Tar-zcvf" $ FILENAME ". tgz $ filename success"
Fi
Rm-F $ filename
Mkdir-p $ cdate
Md5sum $ FILENAME ". tgz"> $ cdate/$ FILENAME ". tgz. MD5"
MV $ FILENAME ". tgz" $ cdate
}

############ Main ##################
Main (){
Low_priority;
Mysql_dump;
If [$? -Ne 0]; then
Return 1
Fi
Rsync_file;
If [$? -Ne 0]; then
Return 1
Fi
Delete_dir;
If [$? -Ne 0]; then
Return 1
Fi
Return 0
}

Main

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.