2019-11-15 13:23:56 +00:00
|
|
|
import logging
|
2019-11-12 11:39:49 +00:00
|
|
|
from subprocess import Popen, PIPE
|
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-04-13 18:12:25 +00:00
|
|
|
def init_repo(repository):
|
2019-11-15 13:23:56 +00:00
|
|
|
"""
|
|
|
|
Attempt to initialize the repository.
|
|
|
|
Doing this after the repository is initialized
|
|
|
|
"""
|
2019-04-13 17:04:54 +00:00
|
|
|
run_command([
|
|
|
|
"restic",
|
|
|
|
"-r",
|
2019-04-13 18:12:25 +00:00
|
|
|
repository,
|
2019-04-13 17:04:54 +00:00
|
|
|
"init",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2019-04-13 18:12:25 +00:00
|
|
|
def backup_volume(repository, volume):
|
2019-04-13 17:04:54 +00:00
|
|
|
run_command([
|
|
|
|
"restic",
|
|
|
|
"-r",
|
2019-04-13 18:12:25 +00:00
|
|
|
repository,
|
2019-04-13 17:04:54 +00:00
|
|
|
"--verbose",
|
|
|
|
"backup",
|
|
|
|
volume.destination,
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2019-04-13 18:12:25 +00:00
|
|
|
def snapshots(repository):
|
2019-04-13 17:04:54 +00:00
|
|
|
run_command([
|
|
|
|
"restic",
|
|
|
|
"-r",
|
2019-04-13 18:12:25 +00:00
|
|
|
repository,
|
2019-04-13 17:04:54 +00:00
|
|
|
"snapshots",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2019-04-13 18:12:25 +00:00
|
|
|
def check(repository):
|
2019-04-13 17:04:54 +00:00
|
|
|
run_command([
|
|
|
|
"restic",
|
|
|
|
"-r",
|
2019-04-13 18:12:25 +00:00
|
|
|
repository,
|
2019-04-13 17:04:54 +00:00
|
|
|
"check",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(cmd):
|
2019-11-15 13:23:56 +00:00
|
|
|
logger.info('cmd: %s', ' '.join(cmd))
|
2019-04-13 17:04:54 +00:00
|
|
|
child = Popen(cmd, stdout=PIPE, stderr=PIPE)
|
|
|
|
stdoutdata, stderrdata = child.communicate()
|
|
|
|
|
|
|
|
if stdoutdata:
|
2019-11-15 15:47:40 +00:00
|
|
|
logger.info(stdoutdata.decode().strip())
|
|
|
|
logger.info('-' * 28)
|
2019-04-13 17:04:54 +00:00
|
|
|
|
|
|
|
if stderrdata:
|
2019-11-15 15:47:40 +00:00
|
|
|
logger.info('%s STDERR %s', '-' * 10, '-' * 10)
|
|
|
|
logger.info(stderrdata.decode().strip())
|
|
|
|
logger.info('-' * 28)
|
|
|
|
|
|
|
|
logger.info("returncode %s", child.returncode)
|
|
|
|
return child.returncode
|