restic.sh (1040B)
1 #!/usr/bin/env bash 2 3 set -o errexit 4 set -o nounset 5 set -o xtrace 6 7 apt-get install -y restic 8 9 # Configure restic repo 10 mkdir -p /root/.config/restic 11 12 cat <<EOF > /root/.config/restic/env 13 # https://restic.readthedocs.io/en/latest/040_backup.html#environment-variables 14 RESTIC_REPOSITORY="$RESTIC_REPOSITORY" 15 RESTIC_PASSWORD="$RESTIC_PASSWORD" 16 EOF 17 18 19 # Install systemd service unit 20 cat <<'EOF' > /etc/systemd/system/restic.service 21 [Unit] 22 Description=Create a backup with restic 23 24 [Service] 25 Type=oneshot 26 EnvironmentFile=/root/.config/restic/env 27 ExecStart=-/usr/bin/restic init 28 ExecStart=-/usr/bin/restic backup /host-nfs 29 ExecStart=-/usr/bin/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 30 ExecStart=-/usr/bin/restic prune 31 32 [Install] 33 WantedBy=default.target 34 EOF 35 36 # Install systemd timer 37 cat <<'EOF' > /etc/systemd/system/restic.timer 38 [Unit] 39 Description=Create a backup with restic 40 41 [Timer] 42 Unit=restic.service 43 OnCalendar=*-*-* 01:03:00 44 45 [Install] 46 WantedBy=timers.target 47 EOF 48 49 systemctl enable restic.timer 50 systemctl start restic.timer