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