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

113 lines
5.1 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
2023-12-18 19:31:57 +01:00
import sys
2023-12-04 22:33:51 +01:00
import argparse
import subprocess
2023-12-18 19:31:57 +01:00
from subprocess import Popen, PIPE
2023-12-12 16:12:12 +01:00
2024-03-09 18:48:05 +01:00
KOPIA_EXEC="/usr/local/bin/kopia"
2023-12-04 22:33:51 +01:00
KOPIA_PASSWORD="{{ params.password|default('') }}"
2023-12-18 19:31:57 +01:00
KOPIA_CONFIG_PATH="/etc/kopia/{{ name }}/repo.config"
2023-12-04 22:33:51 +01:00
2023-12-12 16:12:12 +01:00
ENV = {
2023-12-18 19:31:57 +01:00
"KOPIA_PASSWORD": KOPIA_PASSWORD,
"KOPIA_CONFIG_PATH": KOPIA_CONFIG_PATH,
2023-12-12 16:12:12 +01:00
}
2023-12-04 22:33:51 +01:00
parser = argparse.ArgumentParser(prog='{{ name }} backup')
2023-12-12 16:12:12 +01:00
2023-12-18 19:31:57 +01:00
def init(mode="filesystem", path=None, bucket=None, prefix=None, gateway=None, region=None, ak=None, sak=None):
2024-02-07 22:00:25 +01:00
if mode == "filesystem" and path:
2024-03-09 18:48:05 +01:00
repo_connect = Popen(f"{KOPIA_EXEC} repository connect filesystem --config-file={KOPIA_CONFIG_PATH} --path={path} -p {KOPIA_PASSWORD}", shell=True, stdout=PIPE, stderr=PIPE)
repo_create = Popen(f"{KOPIA_EXEC} repository create filesystem --config-file={KOPIA_CONFIG_PATH} --path={path} -p {KOPIA_PASSWORD}" , shell=True, stdout=PIPE, stderr=PIPE)
2024-02-07 22:00:25 +01:00
elif mode == "s3" and prefix and gateway and region and ak and sak:
2024-03-09 18:48:05 +01:00
cmd_create = f"{KOPIA_EXEC} repository create s3 --config-file={KOPIA_CONFIG_PATH} --bucket={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_EXEC} repository connect s3 --config-file={KOPIA_CONFIG_PATH} --bucket={bucket} --prefix={prefix}/ --endpoint={gateway} --region={region} --access-key={ak} --secret-access-key={sak} -p {KOPIA_PASSWORD} --no-check-for-updates --description='{{ name }} repository'"
2024-02-07 22:00:25 +01:00
run_create = Popen(cmd_create, shell=True, stdout=PIPE, stderr=PIPE)
run_create.wait()
if run_create.returncode == 0:
print("successfully created repository")
run_connect = Popen(cmd_connect, shell=True, stdout=PIPE, stderr=PIPE)
run_connect.wait()
if run_connect.returncode == 0:
print("successfully connected to repository")
else:
print("no valid mode or missing informations")
return None
return True
2023-12-04 22:33:51 +01:00
def set_policy(compression="zstd"):
2024-11-30 14:35:29 +01:00
cmd_policy = f"{KOPIA_EXEC} policy set --global --config-file={KOPIA_CONFIG_PATH} --compression={compression} --keep-latest={{ params.keep_latest|default(7) }} --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 -p {KOPIA_PASSWORD}"
2024-02-07 22:00:25 +01:00
run_policy = Popen(cmd_policy, shell=True, stdout=PIPE, stderr=PIPE)
run_policy.wait()
if run_policy.returncode == 0:
print("successfully set repository policy")
return run_policy.returncode == 0
2023-12-18 19:31:57 +01:00
2024-10-27 12:52:57 +01:00
def set_cache(size=500, limit=1000):
2024-02-07 22:00:25 +01:00
rcs = []
2024-11-30 14:35:29 +01:00
cmds_cache = [f"{KOPIA_EXEC} cache set --content-cache-size-mb {size} --config-file={KOPIA_CONFIG_PATH} -p {KOPIA_PASSWORD}",
f"{KOPIA_EXEC} cache set --content-cache-size-limit-mb {limit} --config-file={KOPIA_CONFIG_PATH} -p {KOPIA_PASSWORD}"]
2024-02-07 22:00:25 +01:00
for cmd_cache in cmds_cache:
run_cache = Popen(cmd_cache, shell=True, stdout=PIPE, stderr=PIPE)
run_cache.wait()
rcs.append(run_cache.returncode == 0)
if all(rcs):
print("successfully set cache policy")
return all(rcs)
2023-12-18 19:31:57 +01:00
def run(before_tasks=[]):
2024-02-07 22:00:25 +01:00
rcs = []
for cmd_task in before_tasks:
run_task = Popen(cmd_task, shell=True)
run_task.wait()
rcs.append(run_task.returncode == 0)
if not all(rcs):
return False
2023-12-18 19:31:57 +01:00
2024-11-30 14:35:29 +01:00
cmd_launch = f"{KOPIA_EXEC} snapshot create {{ params.dirs|map(attribute='path')|join(' ') }} --config-file={KOPIA_CONFIG_PATH} -p {KOPIA_PASSWORD}"
2024-02-07 22:00:25 +01:00
run_launch = Popen(cmd_launch, shell=True)
run_launch.wait()
return run_launch.returncode == 0
2023-12-18 19:31:57 +01:00
def list_snapshots():
2024-11-30 14:35:29 +01:00
cmd_list = f"{KOPIA_EXEC} snapshot list --config-file={KOPIA_CONFIG_PATH} -p {KOPIA_PASSWORD}"
2024-02-07 22:00:25 +01:00
run_list = Popen(cmd_list, shell=True)
run_list.wait()
return run_list.returncode == 0
2023-12-04 22:33:51 +01:00
if __name__ == "__main__":
2024-02-07 22:00:25 +01:00
parser.add_argument('action', nargs="?")
args = parser.parse_args()
res_init = init(mode="s3", bucket="kopia", 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:
print("init error")
sys.exit(1)
if args.action == "run":
if not set_policy():
print("set policy error")
sys.exit(1)
2024-10-27 12:52:57 +01:00
if not set_cache({{ kopia.repos[params.repo].cache_size|default(500) }},{{ kopia.repos[params.repo].cache_limit|default(1000) }}):
2024-02-07 22:00:25 +01:00
print("set cache error")
sys.exit(1)
if not run(before_tasks={{ params.before_tasks|default([]) }}):
print("run error")
sys.exit(1)
elif args.action == "env":
print();print("# source this to use kopia with this repo")
for k,v in ENV.items():
print(f"export {k}=\"{v}\"")
else:
list_snapshots()