The example in this article describes how Python implements the backup directory. Share to everyone for your reference. Specific as follows:
Backup Script 1:
#!/usr/bin/python# Filename:backup_ver1.pyimport osimport time# 1. The files and directories to is backed up is specified in a list.source = ['/home/swaroop/byte ', '/home/swaroop/bin ']# If You is using Windows, use Source = [R ' C:\Documents ', R ' D:\Work ') or something like that# 2. The backup must stored in a main backup Directorytarget_dir = '/mnt/e/backup/' # Remember ll be using# 3. The files is backed up into a zip file.# 4. The name of the ZIP archive is the current date and Timetarget = Target_dir + time.strftime ('%y%m%d%h%m%s ') + '. Zip ' # 5. We Use the Zip command (in unix/linux) to put the files in a zip Archivezip_command = "Zip-qr '%s '%s"% (target, ". Joi N (source)) # Run the backupif os.system (zip_command) = = 0: print ' successful backup to ', Targetelse: print ' backup FAILED '
Output:
$ python backup_ver1.py
Successful backup To/mnt/e/backup/20041208073244.zip
Backup Script 2:
#!/usr/bin/python# Filename:backup_ver2.pyimport osimport time# 1. The files and directories to is backed up is specified in a list.source = ['/home/swaroop/byte ', '/home/swaroop/bin ']# If You is using Windows, use Source = [R ' C:\Documents ', R ' D:\Work ') or something like that# 2. The backup must stored in a main backup Directorytarget_dir = '/mnt/e/backup/' # Remember ll be using# 3. The files is backed up into a zip file.# 4. The current day was the name of the subdirectory in the main directorytoday = Target_dir + time.strftime ('%y%m%d ') # The Cur Rent time is the name of the zip Archivenow = time.strftime ('%h%m%s ') # Create the subdirectory if it isn ' t already thereif Not os.path.exists (today): Os.mkdir (today) # Make directory print ' successfully created directory ', today# the name of The zip Filetarget = today + os.sep + now + '. Zip ' # 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 ' Bac Kup FAILED '
Output:
$ python backup_ver2.py
Successfully created directory/mnt/e/backup/20041208
Successful backup To/mnt/e/backup/20041208/080020.zip
$ python backup_ver2.py
Successful backup To/mnt/e/backup/20041208/080428.zip
Backup Script 3:
#!/usr/bin/python# Filename:backup_ver4.pyimport osimport time# 1. The files and directories to is backed up is specified in a list.source = ['/home/swaroop/byte ', '/home/swaroop/bin ']# If You is using Windows, use Source = [R ' C:\Documents ', R ' D:\Work ') or something like that# 2. The backup must stored in a main backup Directorytarget_dir = '/mnt/e/backup/' # Remember ll be using# 3. The files is backed up into a zip file.# 4. The current day was the name of the subdirectory in the main directorytoday = Target_dir + time.strftime ('%y%m%d ') # The Cur Rent time is the name of the "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 ' # Noti Ce the backslash!# Create the subdirectory if It isn ' t already thereif not os.path.exists (today): Os.mkdir (today) # Make directory print ' successfully created direct Ory ', today# 5. We Use the Zip command (in unix/linux) to put the files in a zip Archivezip_command = "Zip-qr '%s '%s"% (target, ". Joi N (source)) # Run the backupif os.system (zip_command) = = 0:print ' successful backup to ', Targetelse:print ' backup FAILED '
Output:
$ python backup_ver4.py
Enter a comment--Added new examples
Successful backup To/mnt/e/backup/20041208/082156_added_new_examples.zip
$ python backup_ver4.py
Enter a Comment-
Successful backup To/mnt/e/backup/20041208/082316.zip
Hopefully this article will help you with Python programming.