Skip to content

Instantly share code, notes, and snippets.

@floweb
Last active December 12, 2015 16:10
Show Gist options
  • Save floweb/f85111f08e5a2f376a0d to your computer and use it in GitHub Desktop.
Save floweb/f85111f08e5a2f376a0d to your computer and use it in GitHub Desktop.
sync.py - 1000% sure its useless for everyone else
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Website: https://github.com/rndmh3ro/home_backup
# Author: Sebastian Gumprich http://zufallsheld.de
# Modified by: Florian Le Frioux
# Date: 2015
from datetime import datetime
import logging
import os
import click
from fussy import cronlock
from sh import rsync
@click.command()
@click.option('--logfile', default="", help="Specify the logfile to monitor.")
@click.option('--filesfrom', default="", help="Read list of source-file names from FILE.")
@click.option('--src', default="")
@click.option('--dest')
def rsync_to_nas(logfile, filesfrom, src, dest):
"""
Rsync from seedboxes to users' NAS
Usage (usually from crontab):
/usr/bin/python /usr/local/bin/seedbox_sync.py \
--logfile /var/log/seedbox_sync/flo_seedbox_sync.log \
--src /srv/seedbox/ --dest h.floweb.fr:/c/TV/
"""
BACKUP_BASE = ["/srv/seedbox/settings_transmission.json", "/srv/seedbox/sickbeard.db",
"/etc/letsencrypt"]
if "ironist.fr" in dest:
lockpath = '/tmp/iro_seedbox_sync.lock'
elif "floweb.fr" in dest:
lockpath = '/tmp/flo_seedbox_sync.lock'
elif "sachac.fr" in dest:
lockpath = '/tmp/sachac_seedbox_sync.lock'
else:
raise click.Abort
logger = logging.getLogger(__name__)
logger.addHandler(logging.FileHandler(logfile))
lock = cronlock.Lock(lockpath)
with lock:
def log(message):
message = "{} {}".format(datetime.now(), message)
logger.info(message)
click.echo(message)
log("Starting script...")
log("{}, {}, {}, {}".format(logfile, filesfrom, src, dest))
if logfile:
logfile = '--log-file={}'.format(logfile)
if filesfrom:
BACKUP_BASE.append(filesfrom)
filesfrom = [line.rstrip('\n') for line in open(filesfrom) if os.path.exists(line.rstrip('\n'))]
log(filesfrom)
# Do the actual backup
log("Starting rsync...")
if filesfrom:
rsync('-av', logfile, BACKUP_BASE, filesfrom, dest)
else:
os.chdir(src) # Just in case
rsync('-av', logfile, BACKUP_BASE, src, dest)
log("Done !")
if __name__ == '__main__':
rsync_to_nas()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment