1.1、安装yum拓展源percona-release
rpm -ivh http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
1.2、安装percona-xtrabackup
yum install percona-xtrabackup
1.fullbackup.sh 全量备份脚本 addbackup.sh增量备份脚本
[root@ape-bz2009 data]# ls /data/shell/
addbackup.sh fullbackup.sh
2.利用公钥免.ssh登录备份服务器
[root@ape-bz2009 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:P2UyFriljCkYqAaOK+OKaKRiXIxn1iL0/rD/Oacjb6c root@ape-bz2009
The key's randomart image is:
+---[RSA 2048]----+
| |
| . . |
|o . . o |
|=. o + + . |
|+o= o o S + o |
|.= O o o = |
|B B.. o |
|B= .o. +.o . |
|O. .oo=EB |
+----[SHA256]-----+
[root@ape-bz2009 ~]# yum install -y rsync
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): epel/x86_64/updateinfo | 930 kB 00:00:00
(2/2): epel/x86_64/primary | 3.6 MB 00:00:00
Loading mirror speeds from cached hostfile
epel 12696/12696
Package rsync-3.1.2-4.el7.x86_64 already installed and latest version
Nothing to do
[root@ape-bz2009 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.118.172
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.118.172 (192.168.118.172)' can't be established.
ECDSA key fingerprint is SHA256:7f/jSBArNfh5FcBu3lRq0kVUu00oCX2ZiGp6DNWMcL8.
ECDSA key fingerprint is MD5:8f:31:30:c8:a4:86:1e:a9:d3:c6:e0:7f:55:63:47:7d.
Are you sure you want to continue connecting (yes/no)? yees^H^H^[[B^[[B^H
Please type 'yes' or 'no': yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.118.172'"
and check to make sure that only the key(s) you wanted were added.
[root@ape-bz2009 ~]# ssh 192.168.118.172
Last login: Thu Nov 8 15:52:34 2018 from 192.168.118.173
Welcome to Alibaba Cloud Elastic Compute Service !
[root@ape-bz2007 ~]# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:16:3e:13:42:1e brd ff:ff:ff:ff:ff:ff
inet 192.168.118.172/24 brd 192.168.118.255 scope global dynamic eth0
valid_lft 315109603sec preferred_lft 315109603sec
3. 定制备份计划任务
[root@ape-bz2009 ~]# crontab -e
0 1 * * 0 /data/shell/fullbackup.sh
0 * * * * 1-6 /data/shell/addbackup.sh
[root@ape-bz2009 ~]# cd /data/shell/
[root@ape-bz2009 shell]# chmod +x fullbackup.sh
4.执行备份脚本
#!/bin/bash
#定义备份目录
databackupdir=/data/backup
addbackup=/data/addbackup
addbinlog=/data/addbinlog
#判断上条语句是否存在,如果不存在则创建
[ ! -d $databackupdir ] && mkdir $databackupdir
[ ! -d $addbackup ] && mkdir $addbackup
[ ! -d $addbinlog ] && mkdir $addbinlog
#定义备份日志文件
logfile=$databackupdir/mysqlbackup.log
#获取备份目录
#使用innobackupex工具全量备份数据库
innobackupex --defaults-file=/etc/my.cnf --user=root --password='123456' /data/backup
mysqladd=$(ls -lt $databackupdir |sed -n 2p|awk '{print $9}')
innobackupex --defaults-file=/etc/my.cnf --user=root --password='123456' --incremental $addbackup --incremental-basedir=/data/backup/$mysqladd/
#备份binlog日志
n1=`awk '{print $1}' /data/backup/$mysqladd/xtrabackup_binlog_info`
n2=`awk '{print $2}' /data/backup/$mysqladd/xtrabackup_binlog_info`
mysqlbinlog --start-position=$n2 /data/mysql/$n1 > /$addbinlog/`date +%F`.sql
#压缩备份文件
if [ $? -eq 0 ];then
tar zcf $databackupdir
rsync -av $databackupdir [email protected]:/data/mysqlbackup
echo "BackupFileName:$databackupdir.gz"
echo "DataBase Backup Success!"
else
echo "DataBackup Fail!"
fi
增量备份:
#!/bin/bash
#定义备份目录
addbackup=/data/addbackup
#判断上条语句是否存在,如果不存在则创建
[ ! -d $databackupdir ] && mkdir $ addbackup
#定义备份日志文件
logfile=$addbackup/addbackup.log
#使用innobackupex工具增量备份数据库
mysqladd=$(ls -lt $addbackup |sed -n 2p|awk '{print $9}')
filename=/data/addbackup/$mysqladd/xtrabackup_binlog_info
if [ ! -f "$filename" ]
then
echo "Is not increase databak!"
exit
fi
#增量备份数据
innobackupex --defaults-file=/etc/my.cnf --user=root --password='123456' --incremental /data/backup --incremental-basedir=/data/backup/$mysqladd/
#增量备份binlog日志
n1=`awk '{print $1}' /data/addbackup/$mysqladd/xtrabackup_binlog_info`
n2=`awk '{print $2}' /data/addbackup/$mysqladd/xtrabackup_binlog_info`
mysqlbinlog --start-position=$n2 /data/mysql/$n1 > /data/backup/`date +%F`.sql
#压缩备份文件
if [ $? -eq 0 ];then
tar zcf $databackupdir
rsync -av $databackupdir [email protected]:/data/mysqladdbackup
echo "BackupFileName:$databackupdir.gz"
echo "DataBase Backup Success!"
else
echo "Is not incresase databak!"
fi
//查看备份服务器是否备份成功
[root@ape-bz2007 ~]# du -sh /data/mysqlbackup/backup/
117M /data/mysqlbackup/backup/
4.在备份服务器上进行日志清理
#!/bin/bash
#定义备份目录
databackupdir=/data/backup
addbackup=/data/addbackup
sqldelete( )
{
if [ -d $databackupdir ]
then
find $databackupdir -type d -ctime +7 -ok -exec rm {} \;
echo "databackup is delete!"
else
echo "please mkdir databackupdir!"
fi
}
sqladddelete()
{
if [ -d $addbackup ]
then
find $databackupdir -type d -ctime +7 -ok -exec rm {} \;
echo "databackup is delete!"
else
echo "please mkdir addbackup!"
fi
}
sqldelete
sqladddelete
5. 制定代码、静态文件的备份脚本
将以下脚本利用分发系统分发到web服务器上,制定任务计划自动执行。。
#Linux下网站数据定期自动备份与删除脚本
#!/bin/bash
#论坛网页目录自动备份脚本
#论坛数据库自动备份
#自动删除备份目录下面超过10天的文件
#脚本每天早上2点自动执行备份工作
BAKDIR=/data/wwwroot/dedecms.com/
BAKZRLOG=/data/wwwroot/zrlog.com/
BAKDISCUZ=/data/wwwroot/discuz.com/
MAXDAYS=10
find ${BAKDIR} -maxdepth 1 -type f -mtime +$(expr ${MAXDAYS} - 1) -exec echo "Delete {}" \; -exec rm "{}" \;
#将网站目录打包压缩
tar zPcvf /wwwroot/bak/www.dedecms.name_$(date +%F).tar.gz /data/wwwroot/dedecms.com/
find ${BAKZRLOG} -maxdepth 1 -type f -mtime +$(expr ${MAXDAYS} - 1) -exec echo "Delete {}" \; -exec rm "{}" \;
#将网站目录打包压缩
tar zPcvf /wwwroot/bak/www.zrlog.name_$(date +%F).tar.gz /data/wwwroot/zrlog.com/
find ${BAKDISCUZ} -maxdepth 1 -type f -mtime +$(expr ${MAXDAYS} - 1) -exec echo "Delete {}" \; -exec rm "{}" \;
#将网站目录打包压缩
tar zPcvf /wwwroot/bak/www.discuz.name_$(date +%F).tar.gz /data/wwwroot/discuz.com/
rsync -aev $databackupdir [email protected]:/data/wwwrootbak
exit 0
执行任务计划:
#crontab -e
周日备份计划:
0 2 * * * /data/shell/backwwwroot.sh