题目:
周末的时候帮朋友写了一个备份需求的脚本,现在整理一下,分享出来使用Python语言的Fabric模块,这里就不扫盲了,运维必用的Python模块;
大概要求:
公司需求,每天凌晨2点备份数据(数据量不大,每天全备),拷贝至备份服务器,通过md5对比备份文件(本机备份文件和备份服务器文件对比),并将备份情况通知运维组同学。
备份思路:
(1.每天凌晨2点在服务器本地使用tar打包备份文件;
(2.备份成功以后,推送至备份服务器;
(3.校验本地备份文件和备份服务器文件的完整性和一致性;
(4.校验文件是否一致,并将备份状态(成功or失败)写入logs文件;
(5.通过查看logs状态检查备份是否成功,并将内容以邮件形式发送管理员;
(6.日志信息以标签的方式写入文本,通过查看文本的信息。确认是否备份成功;
(7.多台服务器备份,所以备份目录格式为:/backup/ip/
备份流程图:
备份脚本 [server端]
# -*- coding: utf-8 -* #!/usr/bin/env python from fabric.api import * from fabric.colors import * from fabric.context_managers import * from fabric.contrib.console import confirm import time import datetime #backup server env.user = ‘root‘ env.hosts= [‘192.168.64.129‘] env.password= ‘123456‘ env.deploy_version = time.strftime("%Y%m%d") #local backup env.project_dev_source = [‘/usr/local/nginx/‘,‘/usr/local/php/‘] env.project_tar_source = ‘/backup/‘ #backup server env.deploy_project_dir = ‘/backup/‘ env.deploy_address_ip = ‘192.168.64.128‘ env.deploy_address_dir = env.deploy_project_dir + env.deploy_address_ip @task @runs_once def tar_backup(): #在本地打包备份文件 print yellow("Creating backup package...") local("mkdir -p %s" % env.project_tar_source) for source in env.project_dev_source: with lcd(source): local("tar -czf %s-%s.tar.gz . " % ((env.project_tar_source + env.deploy_version),source.split(‘/‘)[-2])) local("find /backup -name ‘*.tar.gz‘ -mtime +7|xargs rm -f") print green("Creating backup package success!") @task def put_package(): #推送至备份服务器 print yellow("Start put package...") run("mkdir -p %s "% env.deploy_address_dir) with settings(warn_only=True): for source in env.project_dev_source: result = put(env.project_tar_source+env.deploy_version+"-"+source.split(‘/‘)[-2]+".tar.gz",env.deploy_address_dir) print green("Put & backup package success!") @task @runs_once def md5_check(): #通过md5对比备份服务器和本地备份文件的完整性 print yellow("check backup package...") with lcd(env.project_tar_source): lmd5 = local("md5sum %s%s*.gz|awk ‘{print $1}‘"% (env.project_tar_source,env.deploy_version),capture=True).split() rmd5 = run("md5sum %s/%s*.gz|awk ‘{print $1}‘"% (env.deploy_address_dir,env.deploy_version)).split() #将备份状态信息写入备份服务器的日志文本 if lmd5==rmd5: status = ‘备份时间 : %-25s备份IP : %-30s备份状态 : 备份成功!‘% (datetime.date.today(),env.deploy_address_ip) run("echo ‘%s‘ >> %slogs/backup_%s.log"% (status,env.deploy_project_dir,env.deploy_version)) print green("backup package md5 contrast success!") else: status = ‘备份时间 : %-25s备份IP : %-30s备份状态 : 备份失败!‘% (datetime.date.today(),env.deploy_address_ip) run("echo ‘%s‘ >> %slogs/backup_%s.log"% (status,env.deploy_project_dir,env.deploy_version)) print green("backup package md5 contrast failure!") @task def back(): tar_backup() put_package() md5_check()
Email脚本 [backup server端]
# -*- coding: utf-8 -* #!/usr/bin/env python import commands import smtplib import string import time #查看备份服务器的日志文本信息 info = commands.getoutput(‘cat /backup/logs/backup_%s.log‘% time.strftime("%Y%m%d")) def email(): HOST = "smtp.qq.com" SUBJECT = "" #收件人 TO = "[email protected]" #发件人 FROM = "[email protected]" #邮件内容 text = "%s" % info BODY = string.join(( "From: %s" % FROM, "To: %s" % TO, "Subject: %s" % SUBJECT , "", text ), "\r\n") #防止一次发送失败,循环发送5次 for i in range(5): try: server = smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login("[email protected]","password") server.sendmail(FROM, [TO], BODY) server.quit() return True except Exception,error: print " \033[31m%s \033[0m"% error continue return False if __name__ == ‘__main__‘: email()
效果测试图,待补充 ...
时间: 2024-11-04 20:23:02