Skip to content

Instantly share code, notes, and snippets.

@marcelblanarik
Created April 5, 2018 17:27
Show Gist options
  • Save marcelblanarik/f5b39c94ff89df6263b98c3307904816 to your computer and use it in GitHub Desktop.
Save marcelblanarik/f5b39c94ff89df6263b98c3307904816 to your computer and use it in GitHub Desktop.
Python files/folders zip and backup
import time
import os
import glob
import shutil
import zipfile
thetime = time.strftime("%Y-%m-%d-%H-%M")
numdays = 86400*14
now = time.time()
FOLDER_PATH = "path/to/your/data"
BACKUP_DIR = "path/to/backup"
dir_name = "some_name" + "_" + thetime
def log(string):
path = BACKUP_DIR + '\\backup.log'
with open(path,'a') as log_file:
log_file.write(time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) + ": " + str(string) + '\n')
log_file.close()
def zip_folder(folder_path, output_path):
"""Zip the contents of an entire folder (with that folder included
in the archive). Empty subfolders will be included in the archive
as well.
"""
parent_folder = os.path.dirname(folder_path)
# Retrieve the paths of the folder contents.
contents = os.walk(folder_path)
try:
zip_file = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
for root, folders, files in contents:
# Include all subfolders, including empty ones.
for folder_name in folders:
absolute_path = os.path.join(root, folder_name)
relative_path = absolute_path.replace(parent_folder + '\\',
'')
zip_file.write(absolute_path, relative_path)
for file_name in files:
absolute_path = os.path.join(root, file_name)
relative_path = absolute_path.replace(parent_folder + '\\',
'')
zip_file.write(absolute_path, relative_path)
except IOError as message:
log(message)
sys.exit(1)
except OSError as message:
log(message)
sys.exit(1)
except zipfile.BadZipfile as message:
log(message)
sys.exit(1)
finally:
zip_file.close()
# Delete old backup files first.
directory=os.path.join(BACKUP_DIR)
for r,d,f in os.walk(directory):
for dir in d:
timestamp = os.path.getmtime(os.path.join(r,dir))
if now-numdays > timestamp:
try:
log("removing" ,os.path.join(r,dir))
shutil.rmtree(os.path.join(r,dir))
except Exception as e:
log(e)
pass
else:
log("Backup files older than %s were deleted." % time.strftime('%c', time.gmtime(numdays)))
# Backup data dirs
log("Copying started...")
shutil.copytree(FOLDER_PATH, BACKUP_DIR + dir_name)
log("Copying finished...")
log("Compressing started...")
zip_folder(BACKUP_DIR + internal_dir_name, dir_name + '.zip')
log("Compressing finished...")
shutil.rmtree(dir_name)
log("Backup job complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment