ansible-edda/playbooks/roles/backups/snapshots/setup/files/syncoid-batch

56 lines
1.8 KiB
Python

#!/usr/bin/env python3
import argparse
import os
import subprocess
import yaml
def load_and_validate_config_dir(config_dir):
if not os.path.isdir(config_dir):
raise ValueError(f"{config_dir} is not a directory")
return [
load_and_validate_config_file(os.path.join(config_dir, file))
for file in os.listdir(config_dir)
]
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")
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
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")
args = parser.parse_args()
config_list = load_and_validate_config_dir(args.config_dir)
for config in config_list:
syncoid_cmd = ["/usr/sbin/syncoid", "--no-sync-snap"]
if config.get("recursive", False):
syncoid_cmd.append("--recursive")
if config.get("skip_parent", False):
syncoid_cmd.append("--skip-parent")
syncoid_cmd.append(config["dataset"])
syncoid_cmd.append(config["backup_dataset"])
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