前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python备份目录脚本

python备份目录脚本

作者头像
py3study
发布2020-01-10 11:07:43
8260
发布2020-01-10 11:07:43
举报
文章被收录于专栏:python3

此脚本适用于备份指定发布目录下的目录,可适当修改运用!

#!/usr/bin/env python #backup app python script. import os import time import sys

nowTime = time.strftime("%Y%m%d")

sourcePath = '/home/zcb/resin-4.0.10/apps/' backupPath = '/home/zcb/tmp/bak'+nowTime

if not os.path.exists('/home/zcb/tmp/bak'+nowTime):  os.mkdir('/home/zcb/tmp/bak'+nowTime)  print time.strftime("%Y-%m-%d")+' Backup Directory create successfully!'

applist = [] def getAppList():  all = os.listdir(sourcePath)  for i in all:   if os.path.isdir(sourcePath+i):    applist.append(i)

def backup_all():  print  "%s  App Backuping...." % time.strftime("%Y-%m-%d")  print "-" * 35  for appName in applist:   os.chdir(sourcePath)   tar_command = 'tar -zcf %s/%s.tar.gz %s' % (backupPath,appName,appName)   if os.system(tar_command) == 0:           print "\033[;32m%s backup successfully!\033[0m" % appName          else:           print "\033[;31m%s backup failed!\033[0m" % appName  print "-" * 35  print "End"

def backup_single():  print  "%s  App Backuping...." % time.strftime("%Y-%m-%d")         print "-" * 35         os.chdir(sourcePath)         tar_command = 'tar -zcf %s/%s.tar.gz %s' % (backupPath,appName,appName)         if os.system(tar_command) == 0:          print "\033[;32m%s backup successfully!\033[0m" % appName         else:          print "\033[;31m%s backup failed!\033[0m" % appName         print "-" * 35         print "End"

if len(sys.argv) == 2:  appName = sys.argv[1]  getAppList()  if appName in applist:   backup_single()  else:   print "%s not exist, Please try again!" % appName else:  input = raw_input("No input any parameters, whether to perform a full backup?(y/n) ")  if input == 'y' or input == 'Y':   getAppList()   backup_all()  else:   print "Exit Backup, Bye!~" #print applist

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档