2021-01-15 13:01:17 +01:00
|
|
|
import argparse
|
|
|
|
import sys
|
2021-01-15 17:58:29 +01:00
|
|
|
from parser import read_specs_file
|
2021-01-15 13:01:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Define parser
|
|
|
|
parser = argparse.ArgumentParser(
|
2021-01-15 14:14:25 +01:00
|
|
|
description="Backup directories and Docker volumes."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--file",
|
|
|
|
action="append",
|
|
|
|
dest="file",
|
2021-01-15 17:58:29 +01:00
|
|
|
required=True,
|
2021-01-15 14:14:25 +01:00
|
|
|
help="File containing spec definitions.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-j",
|
|
|
|
"--json",
|
|
|
|
action="store_const",
|
|
|
|
const=True,
|
|
|
|
default=False,
|
2021-01-15 17:58:29 +01:00
|
|
|
help="Print out the parsed specs as JSON and exit",
|
2021-01-15 14:14:25 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"spec", nargs="*", help="The specs to process. Defaults to all."
|
|
|
|
)
|
2021-01-15 13:01:17 +01:00
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
args = parser.parse_args()
|
2021-01-15 17:58:29 +01:00
|
|
|
specs = sum([read_specs_file(path) for path in args.file], [])
|
2021-01-15 13:01:17 +01:00
|
|
|
|
|
|
|
# Filter specs if needed
|
|
|
|
if args.spec:
|
2021-01-15 17:58:29 +01:00
|
|
|
specs = list(filter(lambda s: s.name in args.spec, specs))
|
2021-01-15 13:01:17 +01:00
|
|
|
|
|
|
|
# Dump parsed data as json
|
|
|
|
if args.json:
|
|
|
|
import json
|
2021-01-15 14:14:25 +01:00
|
|
|
|
2021-01-15 13:01:17 +01:00
|
|
|
print(json.dumps([spec.to_dict() for spec in specs], indent=4))
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Run the backups
|
2021-01-15 17:58:29 +01:00
|
|
|
if not specs:
|
|
|
|
print("No specs, exiting.")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
for spec in specs:
|
|
|
|
spec.backup()
|