ansible-edda/plays/backups/roles/snapshots/files/syncoid-batch

51 lines
1.6 KiB
Plaintext
Raw Normal View History

2022-12-18 00:44:32 +01:00
#!/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", "syncoid_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["syncoid_dataset"])
subprocess.run(syncoid_cmd, check=True)