backup_ver1.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import time
  3. # 1. The files and directories to be backed up are
  4. # specified in a list.
  5. # Example on Windows:
  6. # source = ['"C:\\My Documents"', 'C:\\Code']
  7. # Example on Mac OS X and Linux:
  8. source = ['/Users/swa/notes']
  9. # Notice we had to use double quotes inside the string
  10. # for names with spaces in it.
  11. # 2. The backup must be stored in a
  12. # main backup directory
  13. # Example on Windows:
  14. # target_dir = 'E:\\Backup'
  15. # Example on Mac OS X and Linux:
  16. target_dir = '/Users/swa/backup'
  17. # Remember to change this to which folder you will be using
  18. # 3. The files are backed up into a zip file.
  19. # 4. The name of the zip archive is the current date and time
  20. target = target_dir + os.sep + \
  21. time.strftime('%Y%m%d%H%M%S') + '.zip'
  22. # Create target directory if it is not present
  23. if not os.path.exists(target_dir):
  24. os.mkdir(target_dir) # make directory
  25. # 5. We use the zip command to put the files in a zip archive
  26. zip_command = "zip -r {0} {1}".format(target,
  27. ' '.join(source))
  28. # Run the backup
  29. print "Zip command is:"
  30. print zip_command
  31. print "Running:"
  32. if os.system(zip_command) == 0:
  33. print 'Successful backup to', target
  34. else:
  35. print 'Backup FAILED'