To make a backup of important files:
Ideas:
1. The files and directories that need to be backed up are specified by a list.
2. The backup should be saved in the primary backup directory.
3. File backup as a tar.gz file.
The name of the 4.zip archive is the current date and time.
5. We use the standard tar.gz command.
#!/usr/bin/env python#coding=utf-8#filename: backup_ver2.pyimport osimport time#1. List of backup files source = ['/home/medees/pictures/', '/home/medees/script/'] #If you are Using windows,use source = [r ' C:\Documents ', r ' D:\Work '] or something like that#2. the home directory where the backup files are stored target_dir = '/home/medees/backup/' #Remember to change this to what you will be using#3. the files are backed up into a zip file#4. the day of the main backup directory Today = target_dir + time.strftime ('%y%m%d ') # compressed current time now = 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 + '. tar.gz ' else: target = today + os.sep + now + ' _ ' + comment.replace (' ', ' _ ') + '. tar.gz ' #如果当天日期的目录不存在则创建if not Os.path.exists (Today): os.mkdir (today) print ' successfully created directory ', today#5. we use the zip command (in Unix/linux) to put the files in a zip archiv#zip_command = " zip -qr '%s ' %s % (target, ' '. Join (source)) tar_command = "tar -cvzf %s %s " % (target, ' ". Join (source)) #Run the backupif Os.system (Tar_command)  ==&NBsp;0: print ' Successful backup to ', targetelse: print ' backup failed '
This article is from the "Notes" blog, so be sure to keep this source http://sunflower2.blog.51cto.com/8837503/1546651
Python---backup directories and files