Last active
April 24, 2018 12:22
-
-
Save mightywombat/e141e6c0c881cca473e3de4cce4b770f to your computer and use it in GitHub Desktop.
File Backup & Verify (Mac/*nix)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
import hashlib | |
# Define variables | |
directory = raw_input( "Drag and drop the file or folder you want to back up." ) # Accepts input from user for source file path. | |
directory = directory.rstrip(" ") + "/" # Formats variable. | |
backup_path = raw_input( "Drag and drop the location to which you want to back up." ) # Accepts input from user for destination file path. | |
backup_path = backup_path.rstrip(" ") + "/" # Formats variable. | |
errnum = 0 | |
def md5(filepath): # md5 hash/checksum generator. | |
return hashlib.md5(open(filepath, 'rb').read()).hexdigest() | |
# Creates a file on user's desktop to write/append transfer error filenames. | |
with open( "~/Desktop/backup_error_log.txt", "a" ) as errlog: | |
for file in os.listdir(directory): # For each item in the source directory... | |
shutil.copytree(file, backup_path) # ...copy it to the destination directory... | |
if md5(file) != md5(backup_path + file): # ...and check to make sure the file arrived unchanged. | |
errnum += 1 # If the hashes don't match add 1 to the error variable... | |
errlog.write(file) # ...and record the filename of the file that did not copy correctly. | |
print( "Backup completed! " + errnum + " errors occurred." ) # Notify user when script has finished and advise if there were errors. | |
# The lights dim, the play ends. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed copyfile to copy, to copy2, to copytree. May need to put the md5 check in a separate action.