Required installation:
1. Python
2.7z decompression software
Backup_2.py
Copy codeThe Code is as follows:
# Filename: backup_2.py
'''Backup files.
Version: V2, based on Python 3.3
Usage: backup. py-s: "dir1 | dir2 |..."-t: "target_dir" [-c: "comment"]
-S: The source directories.
-T: The target directory.
-C: Optional, any comment.
Examples:
Backup. py-s: "c: \ src \ F1 | c: \ src \ F2 | c: \ src \ F 3"-t: "c: \ backup"
Backup. py-s: "c: \ src \ F 3"-t: "c: \ backup"-c: "For sample "'''
Import OS
Import sys
Import time
# Read sys. argv
Print (sys. argv)
If len (sys. argv) <2:
Print (_ doc __)
Sys. exit ()
Source = []
Target_dir =''
Comment =''
For arg in sys. argv:
If arg. startswith ('-s :'):
Source = arg [3:]. split ('| ')
Print (source)
Elif arg. startswith ('-t :'):
Target_dir = arg [3:] + OS. sep
Print (target_dir)
Elif arg. startswith ('-c :'):
Comment = arg [3:]
Print (comment)
For I in range (0, len (source )):
Source [I] = "\" "+ source [I] + "\""
Print (source [I])
# Make the file name with the time and comment
Today = target_dir + time. strftime ('% Y % m % D ')
Now = time. strftime ('% H % M % s ')
If len (comment) = 0: # check if a comment was entered
Targetdomaintodaydomainos.sep?now='.7z'
Else:
Target = today + OS. sep + now + '_' + \
Comment. replace (','{}'.7z'
# Create the subdirectory by day
If not OS. path. exists (today ):
OS. mkdir (today) # make directory
Print ('successfully created directory', today)
# Zip command
Zip_command = "7z a % s" % (target, ''. join (source ))
Print (zip_command)
# Run the backup
If OS. system (zip_command) = 0:
Print ('successful backup to ', target)
Else:
Print ('backup failed ')