selfprivacy-rest-api/selfprivacy_api/resources/services/restic.py

51 lines
1.4 KiB
Python
Raw Normal View History

2021-11-11 20:31:28 +02:00
#!/usr/bin/env python3
from flask import request
from flask_restful import Resource
import subprocess
2021-11-16 10:25:44 +02:00
import threading
2021-11-11 20:31:28 +02:00
from selfprivacy_api.resources.services import api
# List all restic backups
class ListAllBackups(Resource):
def get(self):
backupListingCommand = """
restic -r b2:{0}:/sfbackup snapshots --password-file /var/lib/restic/rpass --json
""".format(
request.headers.get("X-Repository-Name")
)
backupListingProcessDescriptor = subprocess.Popen(
backupListingCommand,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
snapshotsList = backupListingProcessDescriptor.communicate()[0]
return snapshotsList.decode("utf-8")
# Create a new restic backup
2021-11-16 10:25:44 +02:00
class AsyncCreateBackup(Resource, threading.Thread):
2021-11-11 20:31:28 +02:00
def put(self):
backupCommand = """
restic -r b2:{0}:/sfbackup --verbose backup /var --password-file /var/lib/restic/rpass
""".format(
request.headers.get("X-Repository-Name")
)
backupProcessDescriptor = subprocess.Popen(
backupCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
return {
"status": 0,
"message": "Backup creation has started",
}
api.add_resource(ListAllBackups, "/restic/backup/list")
2021-11-16 10:25:44 +02:00
api.add_resource(AsyncCreateBackup, "/restic/backup/create")