2019-12-03 00:26:03 +00:00
|
|
|
"""
|
|
|
|
Restic commands
|
|
|
|
"""
|
2019-11-15 13:23:56 +00:00
|
|
|
import logging
|
2019-12-03 03:23:47 +00:00
|
|
|
from typing import List
|
2019-11-12 11:39:49 +00:00
|
|
|
from subprocess import Popen, PIPE
|
2019-12-03 08:40:02 +00:00
|
|
|
from restic_compose_backup import commands
|
2019-04-13 17:04:54 +00:00
|
|
|
|
2019-11-15 13:23:56 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-04-13 17:04:54 +00:00
|
|
|
|
2019-12-03 03:23:47 +00:00
|
|
|
def init_repo(repository: str):
|
2019-11-15 13:23:56 +00:00
|
|
|
"""
|
|
|
|
Attempt to initialize the repository.
|
|
|
|
Doing this after the repository is initialized
|
|
|
|
"""
|
2019-12-03 04:06:28 +00:00
|
|
|
return commands.run(restic(repository, [
|
2019-04-13 17:04:54 +00:00
|
|
|
"init",
|
2019-12-03 04:06:28 +00:00
|
|
|
]))
|
2019-04-13 17:04:54 +00:00
|
|
|
|
|
|
|
|
2019-12-03 03:23:47 +00:00
|
|
|
def backup_files(repository: str, source='/backup'):
|
2019-12-03 04:06:28 +00:00
|
|
|
return commands.run(restic(repository, [
|
2019-04-13 17:04:54 +00:00
|
|
|
"--verbose",
|
|
|
|
"backup",
|
2019-11-29 00:25:00 +00:00
|
|
|
source,
|
2019-12-03 04:06:28 +00:00
|
|
|
]))
|
2019-04-13 17:04:54 +00:00
|
|
|
|
|
|
|
|
2019-12-03 04:00:47 +00:00
|
|
|
def backup_from_stdin(repository: str, filename: str, source_command: List[str]):
|
2019-12-03 03:23:47 +00:00
|
|
|
"""
|
|
|
|
Backs up from stdin running the source_command passed in.
|
|
|
|
It will appear in restic with the filename (including path) passed in.
|
|
|
|
"""
|
2019-12-03 04:06:28 +00:00
|
|
|
dest_command = restic(repository, [
|
|
|
|
'backup',
|
|
|
|
'--stdin',
|
|
|
|
'--stdin-filename',
|
|
|
|
filename,
|
|
|
|
])
|
2019-11-29 00:25:00 +00:00
|
|
|
|
2019-12-03 03:23:47 +00:00
|
|
|
# pipe source command into dest command
|
|
|
|
source_process = Popen(source_command, stdout=PIPE)
|
2019-12-04 00:57:52 +00:00
|
|
|
dest_process = Popen(dest_command, stdin=source_process.stdout, stdout=PIPE, stderr=PIPE)
|
|
|
|
stdout, stderr = dest_process.communicate()
|
|
|
|
|
|
|
|
if stdout:
|
|
|
|
for line in stdout.decode().split('\n'):
|
|
|
|
logger.debug(line)
|
|
|
|
|
|
|
|
if stderr:
|
|
|
|
for line in stderr.decode().split('\n'):
|
|
|
|
logger.error(line)
|
2019-12-03 06:29:52 +00:00
|
|
|
|
|
|
|
# Ensure both processes exited with code 0
|
|
|
|
source_exit, dest_exit = source_process.poll(), dest_process.poll()
|
2019-12-03 06:36:48 +00:00
|
|
|
return 0 if (source_exit == 0 and dest_exit == 0) else 1
|
2019-11-29 00:25:00 +00:00
|
|
|
|
|
|
|
|
2019-12-03 03:23:47 +00:00
|
|
|
def snapshots(repository: str):
|
2019-12-03 04:06:28 +00:00
|
|
|
return commands.run(restic(repository, [
|
2019-04-13 17:04:54 +00:00
|
|
|
"snapshots",
|
2019-12-03 04:06:28 +00:00
|
|
|
]))
|
2019-04-13 17:04:54 +00:00
|
|
|
|
|
|
|
|
2019-12-03 03:23:47 +00:00
|
|
|
def check(repository: str):
|
2019-12-03 04:06:28 +00:00
|
|
|
return commands.run(restic(repository, [
|
2019-04-13 17:04:54 +00:00
|
|
|
"check",
|
2019-12-03 04:06:28 +00:00
|
|
|
]))
|
2019-12-03 04:00:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def restic(repository: str, args: List[str]):
|
|
|
|
"""Generate restic command"""
|
|
|
|
return [
|
|
|
|
"restic",
|
|
|
|
"--cache-dir",
|
|
|
|
"/restic_cache",
|
|
|
|
"-r",
|
|
|
|
repository,
|
|
|
|
] + args
|