Skip to content

Instantly share code, notes, and snippets.

@bsnux
Last active August 29, 2015 13:58
Show Gist options
  • Save bsnux/9936306 to your computer and use it in GitHub Desktop.
Save bsnux/9936306 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
#----------------------------------------------------------
# Create backups using snapshops
#----------------------------------------------------------
from datetime import datetime
import os
# Directory for backups
HOME_DIR = '/Users/arturo/Desktop/'
# Dest directory on external HD
DEST_BACKUP_DIR = '/Volumes/Backup/backups/'
DEST_BACKUP_CURRENT = DEST_BACKUP_DIR + 'current'
DEST_BACKUP_BACKUP = DEST_BACKUP_DIR + 'backup-'
# Directories that will be excluded for backup
EXCLUDE_DIRS = ['Pictures', 'Music']
now = datetime.now().strftime('%Y-%M-%dT%H:%M:%S')
exclude_dirs_cmd = ''
for exclude_dir in EXCLUDE_DIRS:
exclude_dirs_cmd += '--exclude {0} '.format(exclude_dir)
if not os.path.exists(DEST_BACKUP_DIR):
print('Error! {} directory is not valid'.format(DEST_BACKUP_DIR))
exit
print("Syncing directories and files...")
cmd = 'rsync -aP --link-dest={0} {1} {2} {3}'.format(DEST_BACKUP_DIR + 'current',
exclude_dirs_cmd,
HOME_DIR,
DEST_BACKUP_DIR + 'backup-' + now)
os.system(cmd)
# Deleting old current directory
cmd = 'rm -rf {0}'.format(DEST_BACKUP_CURRENT)
os.system(cmd)
# Creating symlink for current directory
cmd = 'ln -s {0}{1} {2}'.format(DEST_BACKUP_BACKUP, now, DEST_BACKUP_CURRENT)
os.system(cmd)
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment