Centos7.4从无到有搭建完整的svn有本地备份远程带密码备份
一、centos7.4 python-pip安装
1.yum -y install epel-release
2.yum install python-pip
二、centos7.4 pexpect安装pip install pexpect -i http://pypi.douban.com/simple –trusted-host pypi.douban.com
1.[WandiscoSVN]
三、centos7.4 SVN安装
1. vim /etc/yum.repos.d/wandisco-svn.repo
name=Wandisco SVN Repobaseurl=http://opensource.wandisco.com/centos/$releasever/svn-1.8/RPMS/$basearch/
enabled=1
gpgcheck=0
yum remove subversion*
2.yum clean all
3.yum install subversion
4.svn –version svn, version 1.8.19 (r1800620)`
5.
四、python 备份脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : www.oldpai.com
import os
import datetime
import commands
import pexpect
svnpath = '/home/svn' #本地svn路径
svnbackuppath = '/home/svnbackup' #本地和远程都备份到这个目录
bugdir = '/opt/zbox/app/zentao/tmp/backup' # bug数据本地存储目录
# #获取目录下的文件列表和目录列表
def get_list(path):
getdirlist = []
getfilelist = []
for root, dirs, files in os.walk(path):
if root == path:
getdirlist,getfilelist = dirs,files
break
return getdirlist, getfilelist
#dirslist:svn的目录列表所有的项目名,fileslist:svn文件列表也是公共配置文件
dirslist, fileslist = get_list(svnpath)
createbackupdirtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S') #获取当前的时间
createdircommand = 'mkdir -p {0}/svn_backup_{1}'.format(svnbackuppath,createbackupdirtime) # 创建备份目录命令
print("创建备份目录命令【{0}】".format(createdircommand))
commands.getoutput(createdircommand) # 创建备份目录
# 拷贝配置文件到备份目录
for file in fileslist:
cpconfigcommand = 'cp {2}/{0} {3}/svn_backup_{1}/'.format(file,createbackupdirtime,svnpath,svnbackuppath)
print("当前copy命令【{0}】".format(cpconfigcommand))
commands.getoutput(cpconfigcommand)
# # =====================本地备份所有项目===========================
for projectname in dirslist:
nowtime = datetime.datetime.now().strftime('%Y%m%d%H%M%S') # 获取当前的时间
command = 'svnadmin hotcopy {2}/{0} {3}/svn_backup_{1}/{0}_{4}'.format(projectname,createbackupdirtime,svnpath,svnbackuppath,nowtime)
print('开始备份项目【{0}】命令【{1}】'.format(projectname,command))
result = commands.getoutput(command)
if result == '':
print("{0}本地备份成功".format(projectname))
else:
print("{0}本地备份失败".format(projectname))
# # =====================远程备份SVN文件===========================
scpcommand = "scp -r {0}/svn_backup_{1} root@ip:{0}/".format(svnbackuppath,createbackupdirtime) # 上传SVN备份文件命令
print(scpcommand)
child = pexpect.spawn(scpcommand, timeout=3600) # 上传超时3600s
child.expect("password")
child.sendline("密码")
child.read()
print 'SVN远程备份完成'
# # =====================远程备份禅道文件===========================
scpbugcommand = "scp -r {0} root@ip:/home/svnbackup/svn_backup_{1}/".format(bugdir, createbackupdirtime)
print(scpbugcommand)
child = pexpect.spawn(scpbugcommand, timeout=3600) # 上传超时3600s
child.expect("password")
child.sendline("密码")
child.read()
print '禅道文件远程备份完成'
# =====================清理本地备份数据===========================
dirslist, fileslist = get_list(svnbackuppath)
timelist = []
for dir in dirslist:
timelist.append(int(dir.split('_')[-1]))
timelist.sort()
if len(timelist)>5:
for i in range(len(timelist)-5):
delfilename = 'svn_backup_'+str(timelist[i]) #需要清除的文件的后缀
delcommand = 'rm -rf {0}/{1}'.format(svnbackuppath,delfilename)
commands.getoutput(delcommand)
print("文件【{0}】清除成功".format(delfilename))
else:
print("当前不需要清除文件")
五、添加定时任务进行定时备份
1. 安装crontab yum install crontabs
2. 查看crontab状态 service crond status
3. 启动 /sbin/service crond start
4. 停止 /sbin/service crond stop
5. 执行添加 crontab -e 定时任务
*/1 * * * * python /home/svn/backup.py 每分钟备份一次