Python backup script

Source: Internet
Author: User

This article refers to the concise python after modifying

#!/usr/bin/pythonimport timeimport os# 1. the files and directories to  be backed up are specified in a list.source = ['/root ',   '/tmp ']# if you are using windows, use source = [r ' c \ Documents ',  r ' D:\Work '] or something like that# 2. the backup  must be stored in a main backup directorytarget_dir =  '/mnt/e/ backup/'  # Remember to change this to what you will be  Using# 3. the files are backed up into a zip file.# 4.  the current day is the name of the subdirectory in the  main directorytoday = target_dir + time.strftime ('%Y%m%d ') # The  current time is The name of the zip archivenow = time.strftime ('%H%M%S ') # Take a  comment from the user to create the name of the zip  filecomment = raw_input (' enter a comment -->  ') If len (comment)  == 0: # check if a comment was entered     target = today + os.sep + now +  '. zip ' else:     target = today + os.sep + now +  ' _ '  +          comment.replace ('   ',  ' _ ')  +  '. Zip '     #  notice the backslash!# create the subdirectory if it isn ' t  Already thereif not os.path.exists (Today):     os.makedirs (today)  #  make directory    print  ' Successfully created directory ',  today# 5. we use  the zip command  (In unix/linux)  to put the files in a  zip archivezip_command =  "zip -qr  '%s '  %s"  %  (target,  '   '. Join (source) # run the backupif os.system (Zip_command)  == 0:     print  ' successful backup to ', targetelse:    print  ' Backup failed '
[[email protected] ~]# python backup.py Enter a comment--weichenrongsuccessful backup to/mnt/e/backup/20161201/1113 19_weichenrong.zip[[email protected] ~]# python backup.py Enter a comment-successful backup to/mnt/e/backup/201612 01/111341.zip[[email protected] ~]# [[email protected] ~]# Ls/mnt/e/backup/20161201/111319_weichenrong.zip 111341.zip


This article is from the "Heaven and Earth Ice Silence" blog, please be sure to keep this source http://yanconggod.blog.51cto.com/1351649/1878401

Python backup 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.