Skip to content

Instantly share code, notes, and snippets.

@alexdebril
Last active May 23, 2022 11:28
Show Gist options
  • Save alexdebril/49bade2e82ab5c500942f74094a4b4af to your computer and use it in GitHub Desktop.
Save alexdebril/49bade2e82ab5c500942f74094a4b4af to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import json
import os
import sys
from shutil import which
if which('rclone') is None:
print("please install rclone")
sys.exit()
HOME = "{}/".format(os.environ['HOME'])
parser = argparse.ArgumentParser()
parser.add_argument("cmd", help='Command: must be init, backup or restore')
parser.add_argument("--resource", help='resource to restore', required=False)
parser.add_argument('--config', help='configuration file', default="{}.config/rclone-sync.json".format(HOME))
args = parser.parse_args()
def run_cmd(cmd):
print(cmd)
os.system(cmd)
def load_config(file):
print("loading configuration from {}".format(file))
f = open(file)
return json.loads(f.read())
def restore(config_file, toRestore):
config = load_config(config_file)
resources = []
found = False
for backup in config['backups']:
for path in backup['paths']:
resource = path['path'].replace(HOME, '')
resources.append(resource)
if resource == toRestore:
print("configuration found, restoring")
found = True
if path['compression']:
archive = "{}.tar.gz".format(resource)
run_cmd("rclone copy {}:{}/{} rclone-restore/".format(backup['remote']['name'], backup['remote']['folder'], archive))
else:
run_cmd("rclone copy {}:{}/{} rclone-restore/{}".format(backup['remote']['name'], backup['remote']['folder'], resource, resource))
if found == False:
print("{} not found, available resources: {}".format(toRestore, resources))
def backup(config_file):
config = load_config(config_file)
print(config)
for backup in config['backups']:
print("backing on {}".format(backup['remote']['name']))
for path in backup['paths']:
resource = path['path'].replace(HOME, '')
if path['compression']:
archive = "{}.tar.gz".format(resource)
run_cmd("tar cvzf {} {}".format(archive, path['path']))
run_cmd("rclone copy {} {}:{}/".format(archive, backup['remote']['name'], backup['remote']['folder']))
run_cmd("rm {}".format(archive))
else:
run_cmd("rclone copy {} {}:{}/{}".format(path['path'], backup['remote']['name'], backup['remote']['folder'], resource))
CONST_DEFAULT_CONFIG = {'backups': (
{
'remote': {
'name': 'main_drive',
'folder': 'rclone-backup'
},
'paths': (
{
'path': 'HOME/Documents',
'compression': False
},
{
'path': 'HOME/.profile',
'compression': False
},
)
},
)}
def init(config_file):
print("initializing configuration in {}".format(config_file))
f = open(config_file, "x")
j = json.dumps(CONST_DEFAULT_CONFIG, indent=3)
f.write(j.replace('HOME', HOME))
f.close()
if args.cmd == "restore":
restore(args.config, args.resource)
elif args.cmd == "backup":
backup(args.config)
elif args.cmd == "init":
init(args.config)
else:
parser.print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment