How Python implements the backup directory

Source: Internet
Author: User
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.

  • Related Article

    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.