2019-01-06 17:34:02 +01:00
|
|
|
import os
|
2020-01-06 23:01:25 +01:00
|
|
|
import logging
|
2020-03-18 21:09:17 +01:00
|
|
|
from subprocess import Popen
|
|
|
|
import asyncio
|
2020-03-19 21:16:38 +01:00
|
|
|
from backive.config.config import Config
|
2019-01-06 17:34:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Backup:
|
2019-01-06 21:29:21 +01:00
|
|
|
def __init__(self, name, cfg=None):
|
2019-02-24 22:42:36 +01:00
|
|
|
self.name = name
|
|
|
|
self.config = cfg
|
2019-01-06 17:34:02 +01:00
|
|
|
|
2020-01-05 13:17:46 +01:00
|
|
|
@classmethod
|
|
|
|
def instance(cls, name, config=None):
|
2020-01-06 23:01:25 +01:00
|
|
|
logging.debug("Backup instance created (%s)", name)
|
2020-01-05 13:17:46 +01:00
|
|
|
return Backup(name, config)
|
|
|
|
|
2019-05-03 21:36:44 +02:00
|
|
|
def get_frequency(self):
|
|
|
|
return self.config.get("frequency", None)
|
2021-05-25 21:32:32 +02:00
|
|
|
|
|
|
|
async def _read_stream(self, stream, cb):
|
|
|
|
while True:
|
|
|
|
line = await stream.readline()
|
|
|
|
if line:
|
|
|
|
cb(line)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
async def stream_subprocess(self, cmd, outcb, errcb):
|
|
|
|
proc = await asyncio.create_subprocess_shell(
|
|
|
|
cmd,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.PIPE,
|
|
|
|
)
|
|
|
|
|
|
|
|
await asyncio.wait([
|
|
|
|
self._read_stream(proc.stdout, outcb),
|
|
|
|
self._read_stream(proc.stderr, errcb)
|
|
|
|
])
|
|
|
|
|
|
|
|
return await proc.wait()
|
2019-05-03 21:36:44 +02:00
|
|
|
|
2020-03-18 21:09:17 +01:00
|
|
|
async def run(self):
|
|
|
|
logging.debug("Running backup %s", self.name)
|
2020-03-19 21:16:38 +01:00
|
|
|
if self.config.get("script", None) is not None:
|
2020-03-18 21:09:17 +01:00
|
|
|
logging.debug("Executing script..")
|
2020-03-19 21:16:38 +01:00
|
|
|
backup_env = os.environ.copy()
|
|
|
|
backup_env["BACKIVE_FROM"] = self.config.get("from")
|
|
|
|
backup_env["BACKIVE_TO"] = self.config.get("to")
|
|
|
|
backup_env["BACKIVE_MOUNT"] = os.path.join(
|
|
|
|
(await Config().get_preferences()).get("mount_root"),
|
|
|
|
(await Config().get_device(
|
|
|
|
self.config.get("target_device")
|
|
|
|
)).config.get("mountname")
|
|
|
|
)
|
2021-05-25 21:32:32 +02:00
|
|
|
backup_env["BACKIVE_TARGET_DIR"] = os.path.join(
|
|
|
|
backup_env["BACKIVE_MOUNT"],
|
|
|
|
backup_env["BACKIVE_TO"]
|
|
|
|
)
|
|
|
|
proc = await self.stream_subprocess(
|
|
|
|
"""mkdir -p {}""".format( backup_env["BACKIVE_TARGET_DIR"]),
|
|
|
|
lambda x: logging.debug("STDOUT: %s", x),
|
|
|
|
lambda x: logging.debug("STDERR: %s", x),
|
2020-03-18 21:09:17 +01:00
|
|
|
)
|
2021-05-25 21:32:32 +02:00
|
|
|
# proc = await asyncio.create_subprocess_shell(
|
|
|
|
# """mkdir -p {}""".format( backup_env["BACKIVE_TARGET_DIR"]
|
|
|
|
# ),
|
|
|
|
# stdout=asyncio.subprocess.PIPE,
|
|
|
|
# stderr=asyncio.subprocess.PIPE,
|
|
|
|
# )
|
|
|
|
# stdout, stderr = await proc.communicate()
|
|
|
|
# logging.debug("stdout: %s", stdout.decode())
|
|
|
|
# logging.debug("stderr: %s", stderr.decode())
|
2020-09-27 17:15:46 +02:00
|
|
|
user = self.config.get("user")
|
2021-05-25 21:32:32 +02:00
|
|
|
# proc = await asyncio.create_subprocess_shell(
|
|
|
|
proc = await self.stream_subprocess(
|
2020-09-27 17:15:46 +02:00
|
|
|
# "set -x; chown -R {} ${{BACKIVE_MOUNT}}/${{BACKIVE_TO}};".format(user) +
|
2021-05-24 23:02:54 +02:00
|
|
|
# "sudo -E -u {} sh -c '".format(user) +
|
2020-03-19 21:16:38 +01:00
|
|
|
self.config.get("script"),
|
2021-05-25 21:32:32 +02:00
|
|
|
lambda x: logging.debug("STDOUT: %s", x),
|
|
|
|
lambda x: logging.debug("STDERR: %s", x),
|
2020-03-19 21:16:38 +01:00
|
|
|
)
|
2021-05-25 21:32:32 +02:00
|
|
|
# "'",
|
|
|
|
# stdout=asyncio.subprocess.PIPE,
|
|
|
|
# stderr=asyncio.subprocess.PIPE,
|
|
|
|
# shell=True,
|
|
|
|
# env=backup_env
|
|
|
|
# )
|
|
|
|
# stdout, stderr = await proc.communicate()
|
|
|
|
# logging.debug("stdout: %s", stdout.decode())
|
|
|
|
# logging.debug("stderr: %s", stderr.decode())
|
|
|
|
return "done"
|