2022-12-18 00:44:32 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
class BatchManager:
|
2022-12-18 00:44:32 +01:00
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
def __init__(self, config_dir):
|
|
|
|
self.__config_list = BatchManager.__load_and_validate_config_dir(config_dir)
|
2022-12-18 00:44:32 +01:00
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
@staticmethod
|
|
|
|
def __load_and_validate_config_dir(config_dir):
|
|
|
|
if not os.path.isdir(config_dir):
|
|
|
|
raise ValueError(f"{config_dir} is not a directory")
|
2022-12-18 00:44:32 +01:00
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
return [
|
|
|
|
BatchManager.__load_and_validate_config_file(os.path.join(config_dir, file))
|
|
|
|
for file in os.listdir(config_dir)
|
|
|
|
]
|
2022-12-18 00:44:32 +01:00
|
|
|
|
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
@staticmethod
|
|
|
|
def __load_and_validate_config_file(config_file_path):
|
|
|
|
if not os.path.isfile(config_file_path):
|
|
|
|
raise ValueError(f"{config_file_path} is not a file")
|
2022-12-18 00:44:32 +01:00
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
with open(config_file_path, encoding="utf-8") as config_file:
|
|
|
|
config = yaml.safe_load(config_file)
|
|
|
|
|
|
|
|
for key in ["dataset", "backup_dataset"]:
|
|
|
|
if key not in config:
|
|
|
|
raise KeyError(f"{key} must be present in {config_file_path}")
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
def __exec(self, source_key, destination_key):
|
|
|
|
for config in self.__config_list:
|
2023-10-01 02:06:19 +02:00
|
|
|
syncoid_cmd = ["/usr/sbin/syncoid", "--no-sync-snap", "--sendoptions=\"p\""]
|
2023-09-30 23:43:27 +02:00
|
|
|
if config.get("recursive", False):
|
|
|
|
syncoid_cmd.append("--recursive")
|
|
|
|
if config.get("skip_parent", False):
|
|
|
|
syncoid_cmd.append("--skip-parent")
|
|
|
|
syncoid_cmd.append(config[source_key])
|
|
|
|
syncoid_cmd.append(config[destination_key])
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.run(syncoid_cmd, check=True,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print(err.stdout.decode("ascii"), flush=True)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def backup(self):
|
|
|
|
self.__exec("dataset", "backup_dataset")
|
|
|
|
|
|
|
|
def restore(self):
|
|
|
|
self.__exec("backup_dataset", "dataset")
|
2022-12-18 00:44:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Backup service data using syncoid")
|
|
|
|
parser.add_argument("--config-dir", type=str, default="/etc/syncoid-batch.d",
|
|
|
|
help="Path to directory with YAML config files")
|
2023-09-30 23:43:27 +02:00
|
|
|
|
|
|
|
subparsers = parser.add_subparsers()
|
|
|
|
|
|
|
|
backup_parser = subparsers.add_parser("backup")
|
|
|
|
backup_parser.set_defaults(func=BatchManager.backup)
|
|
|
|
|
|
|
|
restore_parser = subparsers.add_parser("restore")
|
|
|
|
restore_parser.set_defaults(func=BatchManager.restore)
|
|
|
|
|
2022-12-18 00:44:32 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-09-30 23:43:27 +02:00
|
|
|
args.func(BatchManager(args.config_dir))
|