backup_ver1.py 1.2 KB

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