2019独角兽企业重金招聘Python工程师标准>>>

摘要

由于工作需要,刚好需要这样一个功能的脚本,主要解决:

1. 不想在crontab中调度两条备份任务,一个做全量一个做增量

2. 如果每小时做增量,凌晨4点做全量,那会导致crontab写的复杂,增量需要跳过凌晨4点

3. 自动推备份文件到远程的备份中心服务器

前提

1. 操作系统CentOS,其他没测,当然理论上没问题

2. 本地运行MySQL/MariaDB数据库服务

3. 安装必要的包

3.1 percona-toolkit

3.2 percona-xtrabackup

4. 演示服务器

4.1 10.0.0.2 CentOS 7.3 x86_64 / MariaDB 10.1.21 + percona-toolkit-3.0.2 + percona-xtrabackup-2.3.6 数据库

4.2 10.0.0.3 CentOS 7.3 x86_64 备份

步骤

1. 打通服务器互信

1.1 登录到数据库服务器,在终端执行ssh-keygen -t rsa

# ssh-keygen -t rsa
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:Nwh9Ao11ea3HcRS49wcygYmEfwQxUUL2RVS04QMZHMc root@rx-wj39106
The key's randomart image is:
+---[RSA 2048]----+
|      .B@*oBBO*oo|
|      o+o==.==Eo.|
|      ..o.o. +=o |
|       ..+. +.+o |
|        S.o  +...|
|         . .    o|
|                .|
|                 |
|                 |
+----[SHA256]-----+

1.2 复制公钥到备份服务器ssh-copy-id -i ~/.ssh/id_rsa.pub  root@10.0.0.3

# ssh-copy-id -i ~/.ssh/id_rsa.pub  root@10.0.0.3
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.3's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'root@10.0.0.3'"
and check to make sure that only the key(s) you wanted were added.

1.3 测试登录 ssh root@10.0.0.3

# ssh root@10.0.0.3
Last login: Thu May 18 11:10:47 2017 from 10.0.0.2

第一步完成。

2. 准备备份

2.1 登录到数据库服务器,创建备份专用用户,并授权

# mysql -h 127.0.0.1 -u root -p mysql
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -AWelcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 541
Server version: 10.1.21-MariaDB MariaDB ServerCopyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [mysql]> CREATE USER backup@'127.0.0.1' IDENTIFIED BY 'changeme';
Query OK, 0 rows affected (0.08 sec)MariaDB [mysql]> GRANT SELECT, RELOAD, LOCK TABLES, REPLICATION CLIENT, SHOW VIEW ON *.* TO backup@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)MariaDB [mysql]>

2.2 保存下面脚本为/usr/bin/backup-database.sh

#!/bin/bash
# 数据库自适应备份脚本
# 主要功能对innobackupex做了二次封装,一个脚本实现对整个服务器的全量和增量备份
# 通过crontab调度,可以每小时或者每隔一小时执行,默认在凌晨4点做全备,其他时段
# 做增量备份。
# 脚本采用流的方式推送备份文件到远端服务器,所以需要打通服务器互信
# 当然脚本也获取了当前的星期/月/日,可以自行做其他策略,比如每周一次全备,则只
# 需要在全备的判断中增加对于周的检测# 显示使用帮助
usage () {echo "Usage: ${0} [OPTION...]"echo "  -H, --help          This option displays a help screen and exits."echo "  -n, --compress-threads=#"echo "                      This option specifies the number of worker threads that"echo "                      will be used for parallel compression. It is passed"echo "                      directly to the xtrabackup child process."echo "  -e, --extra-lsndir=name"echo "                      This option specifies the directory in which to save an"echo "                      extra copy of the \"xtrabackup_checkpoints\" file. The"echo "                      option accepts a string argument. It is passed directly"echo "                      to xtrabackup's --extra-lsndir option. See the xtrabackup"echo "                      documentation for details."echo "  -d, --incremental-basedir=name"echo "                      This option specifies the directory containing the full"echo "                      backup that is the base dataset for the incremental"echo "                      backup.  The option accepts a string argument."echo "  -r, --parallel=#    On backup, this option specifies the number of threads"echo "                      the xtrabackup child process should use to back up files"echo "                      concurrently.  The option accepts an integer argument. It"echo "                      is passed directly to xtrabackup's --parallel option. See"echo "                      the xtrabackup documentation for details."echo "  -s, --stream=name   This option specifies the format in which to do the"echo "                      streamed backup.  The option accepts a string argument."echo "                      The backup will be done to STDOUT in the specified"echo "                      format. Currently, the only supported formats are tar and"echo "                      xbstream. This option is passed directly to xtrabackup's"echo "                      --stream option."echo "  -u, --user=name     This option specifies the MySQL username used when"echo "                      connecting to the server, if that's not the current user."echo "                      The option accepts a string argument. See mysql --help"echo "                      for details."echo "  -h, --host=name     This option specifies the host to use when connecting to"echo "                      the database server with TCP/IP.  The option accepts a"echo "                      string argument. See mysql --help for details."echo "  -P, --port=#        This option specifies the port to use when connecting to"echo "                      the database server with TCP/IP.  The option accepts a"echo "                      string argument. See mysql --help for details."echo "  -p, --password=name This option specifies the password to use when connecting"echo "                      to the database. It accepts a string argument.  See mysql"echo "                      --help for details."echo "  -S, --socket=name   This option specifies the socket to use when connecting"echo "                      to the local database server with a UNIX domain socket."echo "                      The option accepts a string argument. See mysql --help"echo "                      for details."echo "  -b, --backup-dir    The local backup directory for temp use"echo "  -R, --remote-server The remote server with SSH where you want to put the backup"echo "                      file into."echo "  -D, --remote-dir    The backup directory on remote server"echo "  -v, --version       Output version information and exit."
}# 读取命令后参数
OPTS=`getopt -o Hvh:P:u:p:S:n:e:d:r:m:R:b:D: --long help,version,host:,port:,user:,password:,socket:,compress-threads:,extra-lsndir:,incremental-basedir:,parallel:,stream:,remote-server:,backup-dir:,remote-dir: -n 'parse-options' -- "$@"`if [ $? != 0 ]
thenexit 1
fieval set -- "$OPTS"# 参数默认值设定
HELP=0
VERSION=0
MYSQL_HOST='127.0.0.1'
MYSQL_PORT=3306
MYSQL_USER='backup'
MYSQL_PASS=''
MYSQL_SOCK=''
INCREMENTAL=0
COMPRESS_THREADS=8
EXTRA_LSNDIR=''
INCREMENTAL_BASEDIR=''
PARALLEL=4
STREAM='xbstream'
REMOTE_SERVER=''
BACKUP_DIR='/var/tmp'
REMOTE_DIR=''# 参数赋值
while true
docase "$1" in-H | --help                ) HELP=1;                 break   ;; # 显示帮助信息,无需解析更多参数直接退出-v | --version             ) VERSION=1;              break   ;; # 显示版本信息,无需解析更多参数直接退出-h | --host                ) MYSQL_HOST=$2;          shift 2 ;; # 备份的主机,默认localhost-P | --port                ) MYSQL_PORT=$2;          shift 2 ;; # 服务端口,默认3306-u | --user                ) MYSQL_USER=$2;          shift 2 ;; # 登录用户,默认backup-p | --password            ) MYSQL_PASS=$2;          shift 2 ;; # 登录密码-S | --socket              ) MYSQL_SOCK=$2;          shift 2 ;; # 嵌套字文件位置-n | --compress-threads    ) COMPRESS_THREADS=$2;    shift 2 ;; # 压缩线程数,默认开启压缩-e | --extra-lsndir        ) EXTRA_LSNDIR=$2;        shift 2 ;; # 检查点信息保存的位置,存在则覆盖-d | --incremental-basedir ) INCREMENTAL_BASEDIR=$2; shift 2 ;; # -r | --parallel            ) PARALLEL=$2;            shift 2 ;; # 并发子进程数量-m | --stream              ) STREAM=$2;              shift 2 ;; # 数据流格式,默认xbstream-R | --remote-server       ) REMOTE_SERVER=$2;       shift 2 ;; # 远程服务器信息,比如root@10.0.0.1-b | --backup-dir          ) BACKUP_DIR=$2;          shift 2 ;; # 本地工作目录,默认/var/tmp-D | --remote-dir          ) REMOTE_DIR=$2;          shift 2 ;; # 远程备份路径,比如/data/backup-- ) shift; break ;;* ) break ;;esac
done# 显示版本
if [[ $VERSION -eq 1 ]]
thenecho "MySQL Adaptive Backup v1.0.0"exit 0
fi# 显示帮助
if [[ $HELP -eq 1 ]]
thenusageexit 0
fi# 对参数进行判断,如果没有提供则报错并退出
if ! [ -n "${EXTRA_LSNDIR}" ]
thenecho "Please specify the action you want to run with -e or -extra_lsndir"exit 1;
fiif ! [ -n "${REMOTE_SERVER}" ]
thenecho "Please specify the action you want to run with -R or --remote-server"exit 1
fiif ! [ -n "${BACKUP_DIR}" ]
thenecho "Please specify the action you want to run with -b or --backup-dir"exit 1
fiif ! [ -n "${REMOTE_DIR}" ]
thenecho "Please specify the action you want to run with -D or --remote-dir"exit 1
fiif ! [ -n "${INCREMENTAL_BASEDIR}" ]
thenecho "Please specify the action you want to run with -d or --incremental-basedir"exit 1
fi# 开始备份
d=$(date +"%Y-%m-%d %H:%M:%S") # 当前时间,写日志需要使用
h=$(date +"%k")                # 当前的小时数,24小时制
d=$(date +"%-d")               # 当前日期的天,可用用于更复杂的备份策略
m=$(date +"%-m")               # 当前日期的月,可以用于更复杂的备份策略
w=$(date +"%u")                # 当前日期的周,可以用于更复杂的备份策略
t=$(date +"%Y_%m_%d_%H_%M")    # 备份文件名上的时间戳echo "[${d}] ----------------------------------------" >> /var/tmp/backup.log# 生成备份命令
CMD="innobackupex --compress"
CMD="${CMD} --host=${MYSQL_HOST}"
CMD="${CMD} --port=${MYSQL_PORT}"
CMD="${CMD} --user=${MYSQL_USER}"
CMD="${CMD} --password=${MYSQL_PASS}"
CMD="${CMD} --compress-threads=${COMPRESS_THREADS}"
CMD="${CMD} --parallel=${PARALLEL}"
CMD="${CMD} --extra-lsndir=${EXTRA_LSNDIR}"
CMD="${CMD} --stream=xbstream"if [[ ${h} -eq 4 ]] # 每天凌晨4:00做全备,其他时间做增量
thenecho "[${d}] Make full backup" >> /var/tmp/backup.log
elseecho "[${d}] Make incremental backup" >> /var/tmp/backup.logCMD="${CMD} --incremental"CMD="${CMD} --incremental-basedir=${INCREMENTAL_BASEDIR}"
fiCMD="${CMD} ${BACKUP_DIR}"
CMD="${CMD} | ssh ${REMOTE_SERVER}"
CMD="${CMD} \"cat - > ${REMOTE_DIR}_${t}.xbs\""
echo "[${d}] ${CMD}" >> /var/tmp/backup.log# 执行备份操作
eval $CMDexit 0

2.3 在数据库服务器端使用系统任务调度

# crontab -l
0 */1 * * * sh /usr/bin/backup-database.sh -p changeme -e /root/backup -R root@10.0.0.3 -D /root/backup -b /root/backup -d /root/backup

测试是每个整点做备份,也可以根据自身的需求更改

2.4 查看日志

备份脚本会写日志到/var/tmp/backup.log,这个后续考虑通过命令行更改

# cat /var/tmp/backup.log
[2017-05-18 04:00:01] ----------------------------------------
[2017-05-18 04:00:01] Make full backup
[2017-05-18 04:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream /root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_04_00.xbs"
[2017-05-18 05:00:01] ----------------------------------------
[2017-05-18 05:00:01] Make incremental backup
[2017-05-18 05:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_05_00.xbs"
[2017-05-18 06:00:01] ----------------------------------------
[2017-05-18 06:00:01] Make incremental backup
[2017-05-18 06:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_06_00.xbs"
[2017-05-18 07:00:01] ----------------------------------------
[2017-05-18 07:00:01] Make incremental backup
[2017-05-18 07:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_07_00.xbs"
[2017-05-18 08:00:01] ----------------------------------------
[2017-05-18 08:00:01] Make incremental backup
[2017-05-18 08:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_08_00.xbs"
[2017-05-18 09:00:01] ----------------------------------------
[2017-05-18 09:00:01] Make incremental backup
[2017-05-18 09:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_09_00.xbs"
[2017-05-18 10:00:01] ----------------------------------------
[2017-05-18 10:00:01] Make incremental backup
[2017-05-18 10:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_10_00.xbs"
[2017-05-18 11:00:01] ----------------------------------------
[2017-05-18 11:00:01] Make incremental backup
[2017-05-18 11:00:01] innobackupex --compress --host=127.0.0.1 --port=3306 --user=backup --password=changeme --compress-threads=8 --parallel=4 --extra-lsndir=/root/backup --stream=xbstream --incremental --incremental-basedir=/root/backup /root/backup | ssh root@10.0.0.3 "cat - > /root/backup_2017_05_18_11_00.xbs"

2.5 登录备份中心服务器查看

# ls -lah
total 1644
-rw-------. 1 root root    880 Feb  7 16:58 anaconda-ks.cfg
-rw-r--r--  1 root root 4.2M May 18 04:01 backup_2017_05_18_04_00.xbs
-rw-r--r--  1 root root 315K May 18 05:01 backup_2017_05_18_05_00.xbs
-rw-r--r--  1 root root 315K May 18 06:01 backup_2017_05_18_06_00.xbs
-rw-r--r--  1 root root 315K May 18 07:01 backup_2017_05_18_07_00.xbs
-rw-r--r--  1 root root 315K May 18 08:01 backup_2017_05_18_08_00.xbs
-rw-r--r--  1 root root 315K May 18 09:01 backup_2017_05_18_09_00.xbs
-rw-r--r--  1 root root 344K May 18 10:01 backup_2017_05_18_10_00.xbs
-rw-r--r--  1 root root 348K May 18 11:01 backup_2017_05_18_11_00.xbs

本文仅是演示目的,目录没有做更多的规划,考虑在生产服务器,具体备份位置看磁盘挂载的位置,一般可能是/opt/data或者/data。

转载于:https://my.oschina.net/u/1261643/blog/903450

一个脚本实现全量增量备份,并推送到远端备份中心服务器相关推荐

  1. Scp全量远程拷贝(推送、拉取)

    参数 -P:指定端口,默认22可不写(P大写) -r:递归拷贝目录 -p:属性不变 -l:限制速度 (如 -l 1024 默认单位M) 源主机:192.168.10.158 源文件:/root/sof ...

  2. mysqldump备份(全量+增量)

    在日常运维工作中,对mysql数据库的备份是万分重要的,以防在数据库表丢失或损坏情况出现,可以及时恢复数据. 线上数据库备份场景: 每周日执行一次全量备份,然后每天下午1点执行MySQLdump增量备 ...

  3. mysqldump全量恢复_【MySQL】全量+增量的备份/恢复

    生产环境中,有时需要做MySQL的备份和恢复工作.因MySQL是在运行过程中的,做全量备份需要时间,全量备份完成后又有数据变动,此时需要增量备份辅助.如果想恢复数据到一个空库(例如数据迁移或者上云等更 ...

  4. MySQL数据库之全量+增量+二进制日志的备份与恢复

    一.简介数据的备份与恢复 1.为什么备份? 灾难恢复:人为错误.硬件故障(冗余).软件故障(bug).自然灾害.黑客攻击.误操作.-: 测试: 2.备份时应该注意些什么? 能容忍最多丢失多少数据: 恢 ...

  5. “全量增量” 与 “增量同步” 一文了解清楚【建议收藏】

    大家在同步数据的时候都会接触到2个名词,"全量增量" 与 "增量同步" ,名字都长得差不多,但是意思和操作却不一样:比如部门领导给你方案,那我们要如何去选择其中 ...

  6. CMS推送,免费CMS推送,全平台CMS自动URL推送

    CMS推送怎样才能做到,CMS推送软件可以帮助我们将链接主动推送,支持各大平台(如图).网站优化说是为了搜索引擎服务,如果我们站在搜索引擎的角度去分析,就不难发现真正的优化是为我们网站用户服务的. C ...

  7. destoon8.0火车头免登陆发布接口+全模块采集规则+主动推送百度

    destoon8.0火车头免登陆发布接口,不限制域名,支持全模块模拟采集更新外加入库自动推送百度. 下载地址:destoon8.0火车头免登陆发布接口+全模块采集规则+主动推送百度-68资源网dest ...

  8. linux svn 增量备份脚本,svn全量和增量备份详解(示例代码)

    1.svn的安装: yum安装: yum install -y subversion mkdir /data/svn svnadmin create project-1 svnadmin create ...

  9. mysql增量脚本_mysql全量和增量备份脚本

    全量: [root@master leo]# cat DBfullBak.sh #!/bin/bash #use mysqldump to fully backup mysql data BakDir ...

最新文章

  1. lightoj 1014
  2. javascript写滑动图片
  3. org.apache.commons.io——FileUtils学习笔记
  4. 普通IO流字符输入输出流文件拷贝
  5. cocos2d-x学习(一) HelloWorld
  6. SSM博客 点赞和文章浏览量实现
  7. 青岛大学计算机科学技术学院图灵班,北京大学第三届图灵班(2018级)开班仪式举行...
  8. 【毕业设计】67-基于单片机的三相电压型SPWM逆变器控制设计及应用(仿真、原理图、源代码、低重复参考文档、答辩PPT、英文文献)
  9. Adapter模式简述
  10. 【毕设论文——必修篇】开题报告要写些什么?这里有参考模板
  11. [4G5G专题-42]:物理层-无线信道的特征:RSRP、SNR、BLER、MCS、CSI、CQI、SI、PMI
  12. 创造101:如果软件测试工程师组团出道,怎样才能站C位?!
  13. Python — “输入直角边求斜边”
  14. linux 磁盘整理命令,Ubuntu磁盘整理命令汇总
  15. 对乔布斯逝世的47个评价
  16. Eclipse一直提示 Loading descriptor for xxx 的问题
  17. python的subprocess模块
  18. fme和python-docx结合实现批量按指定位置插入图片、文本等信息
  19. 将模型轴心点在unity中归正
  20. 干货 | 携程机票前端安卓虚拟机测试集群建设实践

热门文章

  1. hp-ux锁定用户密码_我们如何简化925移动应用程序的用户入门— UX案例研究
  2. skysat重访周期_重访小恶梦
  3. 硬币 假硬币 天平_小东西叫硬币
  4. pc样式在ie8中的bug
  5. 通过项目逐步深入了解Mybatis(四)
  6. 仿照支付宝账单界面--listview分组显示 用来做!发!财树充值交易明细
  7. 正则验证金额大于等于0,并且只到小数点后2位
  8. Android版哆啦A梦连连看游戏源码完整版
  9. Entity Framework 批量插入
  10. RHEL6   Kickstart 无人值守安装