restic-compose-backup/restic_volume_backup/restic.py

118 lines
2.4 KiB
Python
Raw Normal View History

2019-11-15 14:23:56 +01:00
import logging
2019-11-12 12:39:49 +01:00
from subprocess import Popen, PIPE
2019-04-13 19:04:54 +02:00
2019-11-15 14:23:56 +01:00
logger = logging.getLogger(__name__)
2019-11-25 21:21:17 +01:00
def test():
2019-11-29 01:25:00 +01:00
return run_command(['ls', '/backup'])
2019-11-25 21:21:17 +01:00
2019-04-13 19:04:54 +02:00
2019-04-13 20:12:25 +02:00
def init_repo(repository):
2019-11-15 14:23:56 +01:00
"""
Attempt to initialize the repository.
Doing this after the repository is initialized
"""
2019-11-29 01:25:00 +01:00
return run_command([
2019-04-13 19:04:54 +02:00
"restic",
2019-11-29 01:25:00 +01:00
"--cache-dir",
"/restic_cache",
2019-04-13 19:04:54 +02:00
"-r",
2019-04-13 20:12:25 +02:00
repository,
2019-04-13 19:04:54 +02:00
"init",
])
2019-11-29 01:25:00 +01:00
def backup_files(repository, source='/backup'):
return run_command([
2019-04-13 19:04:54 +02:00
"restic",
2019-11-29 01:25:00 +01:00
"--cache-dir",
"/restic_cache",
2019-04-13 19:04:54 +02:00
"-r",
2019-04-13 20:12:25 +02:00
repository,
2019-04-13 19:04:54 +02:00
"--verbose",
"backup",
2019-11-29 01:25:00 +01:00
source,
2019-04-13 19:04:54 +02:00
])
2019-11-29 01:25:00 +01:00
def backup_mysql_all(repository, host, port, user, password, filename):
"""Backup all mysql databases"""
# -h host, -p password
return run_command([
'mysqldump',
'--host',
host,
'--port',
port,
'--user',
user,
'--password',
password,
'|',
'restic',
'backup',
'--stdin',
'--stdin-filename',
'dump.sql'
])
def backup_mysql_database(repository, host, port, user, password, filename, database):
"""Backup a single database"""
pass
def mysql_ping(host, port, username, password):
"""Check if the database is up and can be reached"""
return run_command([
'mysqladmin',
'ping',
'--host',
host,
'--port',
port,
'-user',
username,
'--password',
password,
])
2019-04-13 20:12:25 +02:00
def snapshots(repository):
2019-11-29 01:25:00 +01:00
return run_command([
2019-04-13 19:04:54 +02:00
"restic",
2019-11-29 01:25:00 +01:00
"--cache-dir",
"/restic_cache",
2019-04-13 19:04:54 +02:00
"-r",
2019-04-13 20:12:25 +02:00
repository,
2019-04-13 19:04:54 +02:00
"snapshots",
])
2019-04-13 20:12:25 +02:00
def check(repository):
2019-11-29 01:25:00 +01:00
return run_command([
2019-04-13 19:04:54 +02:00
"restic",
2019-11-29 01:25:00 +01:00
"--cache-dir",
"/restic_cache",
2019-04-13 19:04:54 +02:00
"-r",
2019-04-13 20:12:25 +02:00
repository,
2019-04-13 19:04:54 +02:00
"check",
])
def run_command(cmd):
2019-11-15 14:23:56 +01:00
logger.info('cmd: %s', ' '.join(cmd))
2019-04-13 19:04:54 +02:00
child = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdoutdata, stderrdata = child.communicate()
if stdoutdata:
2019-11-15 16:47:40 +01:00
logger.info(stdoutdata.decode().strip())
logger.info('-' * 28)
2019-04-13 19:04:54 +02:00
if stderrdata:
2019-11-15 16:47:40 +01: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