Last active
April 21, 2016 00:44
-
-
Save Avantol13/dae43795b3217ec5dae1d5e6c32557e6 to your computer and use it in GitHub Desktop.
simple python backup script with command line functionality
This file contains hidden or 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
#!/usr/bin/env python3 | |
''' | |
Created on Apr 20, 2016 | |
@author: alex.vantol | |
BACKUP SCRIPT - w/ CLI | |
Takes a folder and location as input. | |
Outputs a zip file at given location | |
containing the contents of the input folder. | |
''' | |
import os | |
from zipfile import ZipFile | |
from zipfile import ZIP_DEFLATED | |
import argparse | |
def main(): | |
''' | |
Parse arguments from command line and backup a folder | |
''' | |
parser = argparse.ArgumentParser() | |
parser.add_argument('folder_to_backup', help='The path to the folder you want to backup') | |
parser.add_argument('backup_location', help='The path to the folder where you want to save the .zip backup') | |
parser.add_argument('-v', '--verbose', help='Outputs info about backup to command line', action='store_true') | |
args = parser.parse_args() | |
# Only output to console if verbose is specified | |
if args.verbose: | |
print('Beginning backup process on ' + args.folder_to_backup) | |
print('Creating zip here: ' + args.backup_location) | |
zip_file = ZipFile(args.backup_location + '/_backup.zip', 'w', compression=ZIP_DEFLATED) | |
for dirpath, dirs, files in os.walk(args.folder_to_backup): | |
for f in files: | |
fn = os.path.join(dirpath, f) | |
if args.verbose: | |
print('Backing up ' + str(fn)) | |
zip_file.write(fn) | |
# Need to close zip file | |
zip_file.close() | |
if args.verbose: | |
print('Done.') | |
if __name__ == '__main__': | |
main() |
And a Batch script to run this. Now you can use Windows Task Scheduler to schedule this bat for every day, on shutdown, etc.
@echo off
:: VARIABLES
:: Edit these!
set PATH_TO_SCRIPT="path/to/backup.py"
set FOLDER_TO_BACKUP="path/to/folder/to/backup"
set BACKUP_LOCATION="where/to/generate/zip/backup"
:: Call script
py %PATH_TO_SCRIPT% %FOLDER_TO_BACKUP% %BACKUP_LOCATION%
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: