MySQL backup script

Source: Internet
Author: User
Tags mysql backup

#!/bin/bash

# by Jchen

# mysql Backup for MySQL 5.1.60

# 2013-12-02 14:00:00

# Version V1.1

###############################################

# Number of days to keep backups

Day=3

#等待n秒后再执行备份

Wait=60

#是否开启增量备份默认0为开启1为关闭

Open_increase_bak=1

#全备时间点

Default_time=4

#设置不进行备份的时间点

Exclude_time= "0 5"

###############################################

#载入环境变量, otherwise the/usr/local/mysql/bin/mysql path cannot be found

Source/etc/profile

dbip= "127.0.0.1"

Dbuser= "Dbaroot"

Dbpwd= "dbaroot20170315"

Date= ' Date +%f_%h-%m '

Hour= ' Date ' +%h '

port= ' netstat-tunlp|grep mysql |awk ' {print $4} ' |awk-f: ' {print $} '

mycnf= "/ETC/MY.CNF"

Log_file= "/home/msbakscript/logs/xtrabackup.log"

Backup_path= "/usr/local/hero_all_backup"

Mysql_log= "/home/msbakscript/logs/mysql.log"

innobackupex= "/usr/bin/innobackupex"

Bak_log= "/home/msbakscript/logs/mysqlbak.log"

Mysql_path= "/usr/local/mysql/var"

host=$ (awk-f= '/ipaddr=/{print $} '/etc/sysconfig/network-scripts/ifcfg-eth1)

checkpoint=$ (awk '/to_lsn/{print $} '/usr/local/hero_all_backup/xtrabackup_checkpoints 2>/dev/null)

all_bak= ("$HOST" _ "$DATE")

increase_bak= ("$HOST" _ "$DATE" "-increase")

If [$#-eq 0-o $#-eq 1-o $#-eq 2];then

echo "OK" >/dev/null

Else

echo "sh $ allbakfile or sh $ allbakfile incbakfile"

Exit 1

Fi

[!-D ${backup_path}] && mkdir-p ${backup_path}

[!-d/home/msbakscript/logs] && mkdir-p/home/msbakscript/logs

If [-Z ' '/sbin/pidof-s mysqld ' "];then

echo "Mysqld is not running"

Exit 1

Fi

If [' Rpm-qa |grep-c xtrabackup-0.9-2.rhel4 '-eq 1];then

Rpm-e Xtrabackup-0.9-2.rhel4

Fi

if [!-f/usr/bin/innobackupex];then

echo "Xtrabackup is not installed"

Rpm-i--nodeps http://122.228.194.133:8080/percona-xtrabackup-2.0.7-552.rhel5.x86_64.rpm

elif [-f/usr/bin/innobackupex] && [' ps-ef|grep-v grep |grep-c/usr/bin/innobackupex '-ge 1];then

echo "xtrabackup process already exist."

Exit 1

Fi

Complete_bak ()

{

CD ${backup_path}

${innobackupex}--user=${dbuser}--password=${dbpwd}--port=${port}--host=${dbip}--defaults-file=${mycnf}--stream =tar ${backup_path}/2>>${log_file} |gzip >${backup_path}/${all_bak}_${p

Ort}.tar.gz

If [$?-eq 0] && [' tail-10 ' ${log_file} ' | grep-ic ' completed OK '-eq 1];then

Tar zxvfi ${backup_path}/${all_bak}_${port}.tar.gz xtrabackup_checkpoints

echo "${all_bak}_${port}.tar.gz backup successed" >>${bak_log}

Echo-en "ok\n${all_bak}_${port}.tar.gz" >${mysql_log}

sh/home/msbakscript/redis_backup.sh

Chown Nobody.nobody *

Else

echo "${all_bak}_${port}.tar.gz Backup Failed" >>${bak_log}

Echo-e "failure\n${all_bak}_${port}.tar.gz" >${mysql_log}

Exit 1

Fi

}

Complete_bak

Increase_bak ()

{

CD ${backup_path}

if [!-F ${backup_path}/xtrabackup_checkpoints-o-Z ${backup_path}/xtrabackup_checkpoints];then

echo "Xtrabackup_checkpoints does not exist" >>/{log_file}

Complete_bak

Exit 0

Fi

${innobackupex}--user=${dbuser}--password=${dbpwd}--port=${port}--host=${dbip}--defaults-file=${mycnf}-- No-timestamp--incremental--throttle=30 ${backup_path}/${increase_bak}_${port

}--incremental-lsn=${checkpoint} >>${log_file} 2>&1

If [$?-eq 0] && [' tail-10 ' ${log_file} ' | grep-ic ' completed OK '-eq 1];then

Tar zcfi ${increase_bak}_${port}.tar.gz ${increase_bak}/

RM-RF ${increase_bak}_${port}

echo "${increase_bak}_${port}.tar.gz backup successed" >>${bak_log}

Echo-e "ok\n${increase_bak}_${port}.tar.gz" >${mysql_log}

Else

echo "${increase_bak}_${port} Backup Failed" >>${bak_log}

Echo-e "failure\n${increase_bak}_${port}.tar.gz" >${mysql_log}

Exit 1

Fi

}

Increase_bak

Del_bakfile ()

{

For dbfile in ' Find ' ${backup_path}/"-name" [0-9]*.tar.gz "-type f-mtime +${day} '; Do

Rm-f ${dbfile}

Done

}

Del_bakfile

Bakmysql ()

{

For i in ${exclude_time[@]};d o

["${hour}"-eq "$i"] && exit 0

Done

if ["${hour}"-eq "${default_time}"];then

Complete_bak

elif ["${hour}"-ne "${default_time}"-A ${open_increase_bak}-eq 0];then

Increase_bak

Else

Exit 0

Fi

}

Bakmysql

Case $# in

0)

Bakmysql

Del_bakfile

;;

1)

If ["$" = "All"];then

Complete_bak

Del_bakfile

elif ["$" = "Inc"];then

Increase_bak

Del_bakfile

Else

echo "sh $ All or SH $ inc" && exit 0

Fi

;;

Esac

MySQL backup script

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.