restic-compose-backup/restic_volume_backup/restic.py

91 lines
1.9 KiB
Python
Raw Normal View History

"""
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
from restic_volume_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
"""
return commands.run([
2019-04-13 17:04:54 +00:00
"restic",
2019-11-29 00:25:00 +00:00
"--cache-dir",
"/restic_cache",
2019-04-13 17:04:54 +00:00
"-r",
2019-04-13 18:12:25 +00:00
repository,
2019-04-13 17:04:54 +00:00
"init",
])
2019-12-03 03:23:47 +00:00
def backup_files(repository: str, source='/backup'):
return commands.run([
2019-04-13 17:04:54 +00:00
"restic",
2019-11-29 00:25:00 +00:00
"--cache-dir",
"/restic_cache",
2019-04-13 17:04:54 +00:00
"-r",
2019-04-13 18:12:25 +00:00
repository,
2019-04-13 17:04:54 +00:00
"--verbose",
"backup",
2019-11-29 00:25:00 +00:00
source,
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:00:47 +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)
dest_process = Popen(dest_command, stdin=source_process.stdout)
return dest_process.communicate()
2019-11-29 00:25:00 +00:00
2019-12-03 03:23:47 +00:00
def snapshots(repository: str):
return commands.run([
2019-04-13 17:04:54 +00:00
"restic",
2019-11-29 00:25:00 +00:00
"--cache-dir",
"/restic_cache",
2019-04-13 17:04:54 +00:00
"-r",
2019-04-13 18:12:25 +00:00
repository,
2019-04-13 17:04:54 +00:00
"snapshots",
])
2019-12-03 03:23:47 +00:00
def check(repository: str):
return commands.run([
2019-04-13 17:04:54 +00:00
"restic",
2019-11-29 00:25:00 +00:00
"--cache-dir",
"/restic_cache",
2019-04-13 17:04:54 +00:00
"-r",
2019-04-13 18:12:25 +00:00
repository,
2019-04-13 17:04:54 +00:00
"check",
])
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