Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyinfra_borgbackup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def deploy_borgbackup(
borg_args: str = "/",
skip_check: bool = False,
prometheus_file: str | None = None,
**pyinfra_args,
):
"""Deploy borgbackup.

Expand All @@ -39,6 +40,7 @@ def deploy_borgbackup(
user="root",
group="root",
mode="700",
**pyinfra_args,
)

# Setup SSH connection for backup job
Expand All @@ -52,6 +54,7 @@ def deploy_borgbackup(
user="root",
group="root",
mode="600",
**pyinfra_args,
)
except IOError as e:
print(f"ERROR: Could not open SSH key backup: {e}")
Expand All @@ -67,11 +70,13 @@ def deploy_borgbackup(
user="root",
group="root",
mode="600",
**pyinfra_args,
)

apt.packages(
name="Install borgbackup",
packages=["borgbackup"],
**pyinfra_args,
)
# :todo consider requiring a specific borg version?

Expand All @@ -84,6 +89,7 @@ def deploy_borgbackup(
mode="700",
borg_args=borg_args,
prometheus_file=prometheus_file,
**pyinfra_args,
)

if not borg_initialized:
Expand All @@ -92,25 +98,29 @@ def deploy_borgbackup(
commands=[
"export $(xargs < /root/backup.env) && export BORG_RSH='ssh -F /root/.ssh/config -o \"StrictHostKeyChecking=no\"' && borg init --encryption=repokey $DEST1"
],
**pyinfra_args,
)

files.file(
name="Remove old backup cronjob, replaced by systemd timer",
path="/etc/cron.d/borgbackup",
present=False,
**pyinfra_args,
)

backup_service_file = files.put(
src=importlib.resources.files(__package__).joinpath("borgbackup.service"),
dest="/etc/systemd/system/borgbackup.service",
mode="644",
**pyinfra_args,
)
systemd.service(
name="Setup borgbackup service",
service="borgbackup.service",
running=False,
enabled=False,
daemon_reload=backup_service_file.changed,
**pyinfra_args,
)

backup_timer_file = files.template(
Expand All @@ -119,11 +129,13 @@ def deploy_borgbackup(
mode="644",
minute=f"{random.randint(0, 59):02d}",
hour=f"{random.randint(0, 4):02d}",
**pyinfra_args,
)
systemd.service(
name="Setup borgbackup timer",
service="borgbackup.timer",
running=True,
enabled=True,
daemon_reload=backup_timer_file.changed,
**pyinfra_args,
)
Loading