initial commit

This commit is contained in:
Viv 2019-11-29 16:43:21 -08:00
commit 193fee2abe
2 changed files with 66 additions and 0 deletions

14
backup-config.json Normal file
View File

@ -0,0 +1,14 @@
{
"repos": [
{
"name": "knucles/services/gitea",
"paths": ["/home/docker/containers/gitea"]
},
{
"name": "knucles/services/synapse",
"paths": ["/home/docker/containers/synapse"]
}
],
"repo_name_prefix": "b2:vivlim-restic:"
}

52
do-backups.py Executable file
View File

@ -0,0 +1,52 @@
#!/bin/python3
import json
import shlex
import subprocess
restic_bin = '/home/restic/bin/restic'
init_command = "restic -r %s init"
command = "restic -r %s backup --verbose %s"
def do_backup(repo_name, paths):
result = subprocess.run([restic_bin, '-r', repo_name, 'backup', '--verbose'] + paths)
def init_repo(repo_name):
result = subprocess.run([restic_bin, '-r', repo_name, 'init'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
any_errors = len(result.stderr) > 0
already_existed = result.stderr.decode('utf-8').strip().endswith('config already exists')
if any_errors and not already_existed:
print(result.stderr.decode('utf-8').strip())
return False
if already_existed:
print("Repo already exists")
return True
print(result.stdout.decode('utf-8').strip())
print("Repo was created.")
return True
with open('backup-config.json') as json_file:
data = json.load(json_file)
repo_name_prefix = data['repo_name_prefix']
for repo in data['repos']:
repo_name = repo_name_prefix + repo['name']
print('Repo: ' + repo_name)
paths_to_backup = [shlex.quote(p) for p in repo['paths']]
print('Backing up paths: ' + ", ".join(paths_to_backup))
init_result = init_repo(repo_name)
if not init_result:
print('Skipping backup of %s.' % repo_name)
continue
do_backup(repo_name, paths_to_backup)