paulbsd-salt/states/kopia/templates/kopia_backup.py.j2

66 lines
2.9 KiB
Plaintext
Raw Normal View History

2023-12-04 22:33:51 +01:00
{%- from "kopia/map.jinja" import kopia with context -%}
#!/usr/bin/env python3
# vim: ft=python
import argparse
import subprocess
2023-12-12 16:12:12 +01:00
from subprocess import Popen
2023-12-04 22:33:51 +01:00
KOPIA_BUCKET="kopia"
KOPIA_PASSWORD="{{ params.password|default('') }}"
KOPIA_CONFIG_PATH="/etc/kopia/{{ name }}/{{ name }}.config"
KOPIA_KEEP_LAST=7
2023-12-12 16:12:12 +01:00
ENV = {
"KOPIA_PASSWORD": KOPIA_PASSWORD,
"KOPIA_CONFIG_PATH": KOPIA_CONFIG_PATH,
}
2023-12-04 22:33:51 +01:00
parser = argparse.ArgumentParser(prog='{{ name }} backup')
2023-12-12 16:12:12 +01:00
2023-12-04 22:33:51 +01:00
def init(mode="filesystem", path=None, prefix=None, gateway=None, region=None, ak=None, sak=None):
if mode == "filesystem" and path:
2023-12-12 16:12:12 +01:00
repo_connect = Popen(f"kopia repository connect filesystem --config-file={KOPIA_CONFIG_PATH} --path={path} -p {KOPIA_PASSWORD}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
repo_create = Popen(f"kopia repository create filesystem --config-file={KOPIA_CONFIG_PATH} --path={path} -p {KOPIA_PASSWORD}" , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2023-12-04 22:33:51 +01:00
elif mode == "s3" and prefix and gateway and region and ak and sak:
cmd_create = f"kopia repository create s3 --config-file={KOPIA_CONFIG_PATH} --bucket={KOPIA_BUCKET} --prefix={prefix}/ --endpoint={gateway} --region={region} --access-key={ak} --secret-access-key={sak} -p {KOPIA_PASSWORD} --no-check-for-updates --description='{{ name }} repository'"
cmd_connect = f"kopia repository connect s3 --config-file={KOPIA_CONFIG_PATH} --bucket={KOPIA_BUCKET} --prefix={prefix}/ --endpoint={gateway} --region={region} --access-key={ak} --secret-access-key={sak} -p {KOPIA_PASSWORD} --no-check-for-updates --description='{{ name }} repository'"
2023-12-12 16:12:12 +01:00
run_create = Popen(cmd_create, shell=True)
2023-12-04 22:33:51 +01:00
run_create.wait()
2023-12-12 16:12:12 +01:00
run_connect = Popen(cmd_connect, shell=True)
2023-12-04 22:33:51 +01:00
run_connect.wait()
else:
print("no valid mode or missing informations")
return None
return True
def set_policy(compression="zstd"):
cmd_policy = f"kopia policy set --global --config-file={KOPIA_CONFIG_PATH} --compression={compression} --keep-latest={KOPIA_KEEP_LAST} --keep-hourly 0 --keep-daily {{ params.keep_daily|default(7) }} --keep-weekly {{ params.keep_weekly|default(4) }} --keep-monthly {{ params.keep_monthly|default(6) }} --keep-annual 0 --one-file-system=true"
2023-12-12 16:12:12 +01:00
run_policy = Popen(cmd_policy, shell=True)
2023-12-04 22:33:51 +01:00
run_policy.wait()
return
def run():
cmd_launch = f"kopia snapshot create {% for dir in params.dirs %}{{ dir.path }} {% endfor %} --config-file={KOPIA_CONFIG_PATH}"
2023-12-12 16:12:12 +01:00
run_launch = Popen(cmd_launch, shell=True)
2023-12-04 22:33:51 +01:00
run_launch.wait()
return
if __name__ == "__main__":
parser.add_argument('action', nargs="?")
args = parser.parse_args()
res_init = init(mode="s3", prefix="{{ name }}", gateway="{{ kopia.repos[params.repo].gateway|default('gateway.storjshare.io') }}", region="{{ kopia.repos[params.repo].region|default('EU1') }}", ak="{{ kopia.repos[params.repo].ak }}", sak="{{ kopia.repos[params.repo].sak }}")
if not res_init:
sys.exit(1)
set_policy()
if args.action == "run":
run()