This script is suitable for backing up directories under the specified publishing directory and can be modified appropriately!
#!/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 "-" *
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"-"*
print" End "
Def backup_single ():
print "%s App backuping ..."% time.strftime ("%y-%m-%d")
print "-" *
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"-"*
print "End"
If Len (sys.argv) = = 2:
AppName = sys.argv[1]
Getapplist ()
If AppName in Applist:
Backup_single ()
Else
Print "%s not exist, try again!"% appName
Else
input = raw_input ("No input any parameters, whether-perform a full backup?" y/n) ")
if input = = ' Y ' or input = = ' Y ':
Getapplist ()
Backup_all ()
Else
Print "Exit Backup, bye!~"
#print applist
This article is from the "clown drama" blog, please be sure to keep this source http://smallrookie.blog.51cto.com/9146983/1552670
Python backup directory Script