Python backup directory Script

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.