qweewq123qwe 发表于 2018-1-11 18:51:13

python实现备份gitlab版本库并更改文件名

#!/usr/bin/env python  
#
-*- coding:utf-8 -*-  

  

  
import time
  
import os
  
import commands
  
import subprocess
  
import smtplib
  
from email.mime.text import MIMEText
  
from email.utils import formataddr
  

  

  
class Svnbackup(object):
  '''

  svn svnbackup and save api for thi>  '''
  

  def __init__(self, *arg):
  pass
  

  def sendmail(self, *arg):
  # mail content
  msg = MIMEText(arg, 'plain', 'utf-8')
  # maill address
  msg['From'] = formataddr(["SVN邮件提醒", '发件人邮箱'])
  # receive maill address
  msg['To'] = formataddr(['运维组邮箱', '收件人邮箱'])
  # mail topic
  msg['Subject'] = arg
  

  # The mail SendServer
  server = smtplib.SMTP("smtp.163.com", 25)
  server.login("发件人邮箱", "发件人邮箱密码")
  server.sendmail('发件人邮箱', ['收件人邮箱', ], msg.as_string())
  server.quit()
  

  def runback(self):
  # del old version for the svn in the backsvn server
  starttime = time.time()
  back_status = subprocess.call(
  'cd /home/git/gitlab && PATH=/usr/local/bin:/usr/bin:/bin bundle exec rake gitlab:backup:create RAILS_ENV=production CRON=1 >/dev/null 2>&1',
  shell=True)
  endtime = time.time()
  

  # cost how many seconds
  costtime = (endtime - starttime)
  

  str_time = str(endtime).split('.')
  

  float_time = float(str_time)
  

  ltime = time.localtime(float_time)
  

  timestr = time.strftime("%Y-%m-%d %H:%M:%S", ltime)
  

  mail_contentinfo = '%s 总耗时:%d秒 结束时间:%s' % ('研发源gitlab(172.16.50.44)备份成功,请确认!', costtime, timestr)
  mail_error_contentinfo = '%s ' % ('研发源SVN(172.16.50.44)备份失败,请联系SA刘辉煌!')
  if os.path.exists("/home/git/gitlab/tmp/backups/%s_gitlab_backup.tar" % (str_time)):
  os.rename("/home/git/gitlab/tmp/backups/%s_gitlab_backup.tar" % (str_time),
  "/home/git/gitlab/tmp/backups/%s_gitlab_backup.tar" % (timestr))
  

  if back_status == 0:
  self.sendmail('%s' % mail_contentinfo, 'Gitlab备份成功!')
  else:
  self.sendmail('%s' % mail_error_contentinfo, 'Gitlab备份失败')
  

  def runserver(self):
  self.runback()
  

  

  
if __name__ == '__main__':
  backupsvn = Svnbackup()
  backupsvn.runserver()
  # backupsvn.modify_name()
页: [1]
查看完整版本: python实现备份gitlab版本库并更改文件名