restic-compose-backup/restic-backup/backup.py

55 lines
1.2 KiB
Python
Raw Normal View History

2019-04-13 17:04:54 +00:00
import os
import sys
from containers import RunningContainers
import restic
cmds = ['volumes', 'backup', 'snapshots', 'check']
class Config:
2019-04-13 18:12:25 +00:00
repository = os.environ['RESTIC_REPOSITORY']
2019-04-13 17:04:54 +00:00
password = os.environ['RESTIC_PASSWORD']
@classmethod
def check(cls):
2019-04-13 18:12:25 +00:00
if not cls.repository:
2019-04-13 17:04:54 +00:00
raise ValueError("CONTAINER env var not set")
if not cls.password:
raise ValueError("PASSWORD env var not set")
def main():
if len(sys.argv) < 2:
raise ValueError("Missing argument: {}".format(cmds))
mode = sys.argv[1]
if mode not in cmds:
raise ValueError("Valid arguments: {}".format(cmds))
Config.check()
containers = RunningContainers()
if mode == 'volumes':
volumes = containers.volume_mounts()
for vol in volumes:
2019-04-13 18:12:25 +00:00
# print(vol)
2019-04-13 17:04:54 +00:00
print(vol.mount_string())
binds = containers.bind_mounts()
for vol in binds:
print(vol.mount_string())
if mode == 'backup':
2019-04-13 18:12:25 +00:00
restic.init_repo(Config.repository)
2019-04-13 17:04:54 +00:00
for vol in containers.backup_volumes():
2019-04-13 18:12:25 +00:00
restic.backup_volume(Config.repository, vol)
2019-04-13 17:04:54 +00:00
if mode == 'snapshots':
2019-04-13 18:12:25 +00:00
restic.snapshots(Config.repository)
2019-04-13 17:04:54 +00:00
if __name__ == '__main__':
main()