|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +# 1. The files and directories to be backed up are |
| 5 | +# specified in a list. |
| 6 | +# Example on Windows: |
| 7 | +# source = ['"C:\\My Documents"', 'C:\\Code'] |
| 8 | +# Example on Mac OS X and Linux: |
| 9 | +source = ['/Users/swa/notes'] |
| 10 | +# Notice we had to use double quotes inside the string |
| 11 | +# for names with spaces in it. |
| 12 | + |
| 13 | +# 2. The backup must be stored in a |
| 14 | +# main backup directory |
| 15 | +# Example on Windows: |
| 16 | +# target_dir = 'E:\\Backup' |
| 17 | +# Example on Mac OS X and Linux: |
| 18 | +target_dir = '/Users/swa/backup' |
| 19 | +# Remember to change this to which folder you will be using |
| 20 | + |
| 21 | +# Create target directory if it is not present |
| 22 | +if not os.path.exists(target_dir): |
| 23 | + os.mkdir(target_dir) # make directory |
| 24 | + |
| 25 | +# 3. The files are backed up into a zip file. |
| 26 | +# 4. The current day is the name of the subdirectory |
| 27 | +# in the main directory. |
| 28 | +today = target_dir + os.sep + time.strftime('%Y%m%d') |
| 29 | +# The current time is the name of the zip archive. |
| 30 | +now = time.strftime('%H%M%S') |
| 31 | + |
| 32 | +# Take a comment from the user to |
| 33 | +# create the name of the zip file |
| 34 | +comment = input('Enter a comment --> ') |
| 35 | +# Check if a comment was entered |
| 36 | +if len(comment) == 0: |
| 37 | + target = today + os.sep + now + '.zip' |
| 38 | +else: |
| 39 | + target = today + os.sep + now + '_' + \ |
| 40 | + comment.replace(' ', '_') + '.zip' |
| 41 | + |
| 42 | +# Create the subdirectory if it isn't already there |
| 43 | +if not os.path.exists(today): |
| 44 | + os.mkdir(today) |
| 45 | + print('Successfully created directory', today) |
| 46 | + |
| 47 | +# 5. We use the zip command to put the files in a zip archive |
| 48 | +zip_command = "zip -r {0} {1}".format(target, |
| 49 | + ' '.join(source)) |
| 50 | + |
| 51 | +# Run the backup |
| 52 | +print("Zip command is:") |
| 53 | +print(zip_command) |
| 54 | +print("Running:") |
| 55 | +if os.system(zip_command) == 0: |
| 56 | + print('Successful backup to', target) |
| 57 | +else: |
| 58 | + print('Backup FAILED') |
0 commit comments