Topic:
On the weekend to help friends write a backup requirements of the script, now tidy up, share the use of the Python language fabric module, here is not literacy, operation must use the Python module;
Approximate requirements:
Company needs, daily 2 o'clock in the morning backup data (small amount of data, full daily), copy to the backup server, by MD5 compared to backup files (local backup files and backup server files compared), and the backup situation to inform the Operations Group classmate.
Backup ideas:
(1. Use the tar to package the backup file on the server locally 2 o'clock in the morning every day;
(2. After the backup is successful, push to the backup server;
(3. Verify the integrity and consistency of local backup files and backup server files;
(4. Verify that the files are consistent and write the backup status (success or failure) to the logs file;
(5. Check the logs status to see if the backup was successful and send the content as a message to the administrator;
(6. Log information is written to the text as a label, by viewing the information of the text. Confirm the backup is successful;
(7. Multiple server backups, so the backup directory format is:/backup/ip/
Backup Flowchart:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/70/50/wKiom1W16cjBdiAdAAGdW2fvtVs937.jpg "title=" Server backup flowchart. jpg "alt=" wkiom1w16cjbdiadaagdw2fvtvs937.jpg "/>
backup script [Server side]
# -*- coding: utf-8 -*#!/usr/bin/env pythonfrom fabric.api import * from fabric.colors import *from fabric.context_managers import *from Fabric.contrib.console import confirmimport timeimport datetime#backup serverenv.user = ' root ' env.hosts= [' 192.168.64.129 ']env.password= ' 123456 ' env.deploy_version = time.strftime ("%y%m%d") #local backupenv.project_dev_source = ['/usr/local/nginx/', '/usr/ local/php/']env.project_tar_source = '/backup/' #backup serverenv.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_oncedef tar_backup (): #在本地打包备份文件 print yellow ("Creating backup package ... ") &nbSp; 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! ") @taskdef 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_oncedef 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 () #将备份状态信息写入备份服务器的日志Text if lmd5==rmd5: status = ' Backup time : %-25s backup ip : %-30s backup status : Backup succeeded! '% (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 = ' Backup time : %-25s backup ip : %-30s Backup status : backup failed! '% (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!") @taskdef back (): tar_backup () put_package () md5_check ()
Email script [backup server side]
# -*- coding: utf-8 -*#!/usr/bin/env pythonimport commandsimport smtplibimport stringimport time# View the log text information for the backup server 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") #防止一次发送失败, loop 5 times for i in range (5): try: server = smtplib. SMTP () server.connect (HOST, "") server.starttls () server.login ("[email protected]", "password") server.sendmail (FROM, [TO], BODY) &nbSp; server.quit () return true except Exception,error: print " \033[31m%s \033[0m "% error continue return falseif __name__ == ' __main__ ': email ()
Effect test chart, to be added ...
This article is from the "ˉt, Stand Wentao ┃﹎" blog, please be sure to keep this source http://hypocritical.blog.51cto.com/3388028/1679006
Writing Linux automatic backup scripts with Python