TortoiseSVN备份、还原(附自动化Python脚本)
一、 SVN备份
-
进入svn安装路径bin文件夹下,使用 cd 命令。
在windows下,win+R 键入 cmd 回车 打开命令窗口cmd,进入下列目录(svn服务器安装目录bin):
“c:\Program Files\VisualSVN Server\bin”
-
备份/导出(svnadmin dump C:\Repositories\需导出库 > E:**文件夹\full.svn.bak)
输入下列命令并执行:svnadmin dump D:\Repositories\shujuzu > E:\svnbak\full.svn.bak
根据自己安装配置路径灵活选择变化
-
full.svn.bak为备份文件。将其文件拷贝到目标机器,或是存储备份。
二、SVN导入/还原:
- 在目标机器创建仓库:
svnadmin create D:/repositories/TestProject ,此处TestProject是仓库名
也可右键管理Server新建
注意检查是否需要修改文件夹权限
-
执行命令svnadmin load:
svnadmin load D:/repositories/TestProject < E:\svnbak\full.svn.bak
三、自动化脚本实现
默认大家已有python基础
-
主备份Svn_backup.py
/* 实现功能: 1、svn备份,压缩 2、每天备份,每月独立存放 3、备份异常时发送邮件或短信 */ # !/usr/bin/python # -*- coding: UTF-8 -*- __author__ = 'qxxz' import base64 import threading import os import time, datetime import sys, shutil reload(sys) sys.setdefaultencoding("utf-8") #此处发送邮件代码见sendemail.py from sendemail import emailThread #此处发送短信代码调用公司短信API,封装成了send_sys from sendmessage import send_sys class svnBakThread(threading.Thread): def __init__(self, savepath, savepathMonth, zipmima, circle): threading.Thread.__init__(self) self.savepath = savepath # 天备份保存路径 self.savepathMonth = savepathMonth # 月备份保存路径 self.zipmima = zipmima #压缩密码 self.circle = circle #备份周期(天) # circle = 30 def run(self): runlog = '' # 年月日 now = time.strftime('%Y-%m-%d') # now = time.strftime('%Y-%m-%d %H:%M:%S') bakpath = os.path.join(self.savepath, now) # 备份命令 bak_command ='svnadmin dump D:\Repositories\shujuzu > "' + bakpath + '\\' + 'svn_' + now + '.bak"' # 调用“好压”压缩软件进行压缩 yasuo_command = 'haozipc a -tzip -p' + self.zipmima + ' "' + bakpath + '/' + 'svn_' + now + '.zip" "' + bakpath + '/*"' #目录不存在则创建 if not os.path.exists(bakpath): os.makedirs(bakpath) if os.system(bak_command.encode("gb2312")) == 0: runlog = runlog + u'SVN备份命令执行成功;' if os.system(yasuo_command.encode("gb2312")) == 0: runlog = runlog + u'SVN备份压缩成功;' else: runlog = runlog + u'SVN备份压缩失败;' # 删除备份周期之前的备份信息 # 获取n天之前的日期 beforen = datetime.datetime.now() + datetime.timedelta(days=-self.circle) beforeday = beforen.strftime('%Y-%m-%d') filelist = os.listdir(self.savepath) for f in filelist: if f < beforeday: shutil.rmtree(os.path.join(self.savepath, f)) # 判断是否是月初第一天,月初第一天备份则放入专门地方 if now[-2:] == '01': if not os.path.exists(self.savepathMonth): os.makedirs(self.savepathMonth) shutil.move(bakpath, self.savepathMonth) else: runlog = runlog + u'SVN备份命令执行失败;' if runlog.find(u'失败')==-1: backupresult=u'<span style="font-size:16px;color:red;"><b>SVN备份完成!</b></span>' # 删除压缩前文件 try: os.remove(bakpath + '\\' + 'svn_' + now + '.bak') except: pass else: backupresult=u'<span style="font-size:16px;color:red;"><b>SVN备份失败!</b></span>' # 发送邮件给管理员通知备份情况,函数 emailThread() 代码见 sendemail.py try: # 发件箱、收件箱账号 mail1 = emailThread('fromUser@xxx.com', ['toUser@xxx.com'], self.orclSid + u'svn备份情况', u'你好, <br> ' +backupresult+'<br>备份结果如下:<br>' + runlog, []) mail1.start() except: print u"邮件发送失败!" #短信发送(调用公司短信API发送短信) try: send_sys("亲爱的管理员,SVN备份出现异常,请及时检查一下奥。","137*******24") except: print u"短信发送失败!" if __name__ == '__main__': #此处通过路径映射保存到公司内网外挂磁盘上 svnBakThread(u'\\\\192.168.10.107\\DataBackup\\SVN-Data\\SVN备份\\天备份文件', u'\\\\192.168.10.107\\DataBackup\\SVN-Data\\SVN备份\\月备份文件', 'SvnYasuoMima',6).start()
-
发送邮件代码 sendemail.py
# -*- coding: cp936 -*- __author__ = 'qxxz' ''' 发送带附件邮件 ''' from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib import threading, os class emailThread(threading.Thread): def __init__(self, frommail, tomailList, mailsubject, mailcontent, attachList): threading.Thread.__init__(self) self.frommail = frommail # 发件人 self.tomailList = tomailList # 收件人列表 self.mailsubject = mailsubject # 邮件标题 self.mailcontent = mailcontent # 邮件内容-文本 self.attachList = attachList # 附件列表【‘d:\1.txt’,‘d:\2.txt’】 def run(self): # 创建一个带附件的实例 msg = MIMEMultipart() #添加附件列表 if self.attachList: for attach in self.attachList: if os.path.isfile(attach): # 是个文件时才操作 # 获取文件名 filename = os.path.basename(attach) # 构造附件 att1 = MIMEText(open(attach, 'rb').read(), 'base64', 'gb2312') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename=' + filename # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 msg.attach(att1) # 加邮件头 msg['to'] = ",".join( self.tomailList ) msg['from'] =self.frommail msg['subject'] = self.mailsubject msg.attach(MIMEText(self.mailcontent, _subtype='html', _charset='utf-8')) # msg['context'] = self.mailsubject # 发送邮件 try: server = smtplib.SMTP() server.connect('mail.xxx.com') #邮箱服务器 server.login('发件邮箱账号', '发件邮箱密码') server.sendmail(msg['from'], msg['to'], msg.as_string()) server.quit() server.close() print '发送成功' except Exception, e: print str(e) if __name__ == '__main__': mail1 = emailThread('dbbackup@xxx.com',['toUser@xxx.com'], u'数据库备份情况',u'你好, <br> 数据库备份完成!!!', ['d:\\1.txt','d:\\2.txt']) mail1.start()
-
发送短信sendmessage.py
#coding=utf-8 __author__ = 'qxxz' import sys,datetime import urllib2,urllib def send_sys(message,phonelist): try: #根据公司短信API开酌情写代码 #例子 {"message":"【****】:*******","phonelist":"13833395620","callsystem":"101"} url="http://192.168.10.207:8086/api/sms" data={'message':message,'phonelist':phonelist,'callsystem':'101'} data = urllib.urlencode(data) response = urllib2.urlopen(url, data) res = response.read() except: res=None return res # res=send_sys("老大,我不舒服了。备份出现异常,请立即检查。","137******24") # print res
综上,相关知识点有:
- SVN备份压缩命令
- “好压”压缩软件命令
- 邮箱发送
- 短信发送
- 定时任务设定