Hello everyone.
Lately I noticed, that my BorgWarehouse server did not update the storage and status information, leading to false notification emails being sent for missing backups.
I usually don’t check the main page and changelogs, since I automate the update of the docker containers using Ansible, which led to the issue.
Apparently there was a switch to supercronic as the new service for cron jobs in version 3.2.0.
This is great and all, but the image that the new docker-compose.yml file points to for supercronic, does not work in my case. I don’t know why, but I didn’t really investigate further.
So, rather than building the supercronic container myself, I created a script and execute it hourly using systemd.
Which means, this post will cover the BorgWarehouse update, using curl and systemd script execution.
Let’s begin.
Creating the script and env file.
First things first. We need to provide the BorgWarehouse container with a “new” variable. CRONJOB_KEY. This will be used as the token to authenticate and refresh the status and storage using the existing API.
Just add the new variable to your docker file under environment or if you use Ansible under env.
This is an example of my Ansible task. Use the official docker-compose.yml file from github, if you want to deploy it fresh.
- name: Create borgwarehouse
docker_container:
name: borgwarehouse
image: borgwarehouse/borgwarehouse:latest
networks:
- name: backup
ipv4_address: 172.32.91.200
pull: yes
state: started
restart_policy: always
published_ports:
- 0.0.0.0:2222:22
volumes:
- /etc/borgwarehouse/app/config:/home/borgwarehouse/app/config
- /etc/borgwarehouse/ssh:/home/borgwarehouse/.ssh
- /etc/borgwarehouse/sshd:/etc/ssh
- /etc/borgwarehouse/repos:/home/borgwarehouse/repos
- /etc/borgwarehouse/tmp:/home/borgwarehouse/tmp
- /etc/borgwarehouse/logs:/home/borgwarehouse/logs
env:
WEB_SERVER_PORT: "3000"
SSH_SERVER_PORT: "2222"
FQDN: "{{ borgwarehouse_fqdn }}"
NEXTAUTH_URL: "{{ borgwarehouse_nextauth_url }}"
NEXTAUTH_SECRET: "{{ borgwarehouse_nextauth_secret }}"
BETTER_AUTH_SECRET: "{{ borgwarehouse_better_auth_secret }}"
CRONJOB_KEY: "{{ borgwarehouse_cronjob_key }}"
Once we have that, we can create an env file with the CRONJOB_KEY on the server. I created it in /etc/borgwarehouse/borgwarehouse-cron.env, but there are probably better locations for this.
We write the key into the file using echo and tee, and change the permission to read-only for the root user using chmod.
$ echo 'CRONJOB_KEY="<your key>"' | sudo tee /etc/borgwarehouse/borgwarehouse-cron.env > /dev/null ; sudo chmod 400 /etc/borgwarehouse/borgwarehouse-cron.env
Now we can create the script. I will put this in the same folder.
$ sudo vim /etc/borgwarehouse/borgwarehouse-cron.sh
#!/usr/bin/env bash
# /etc/borgwarehouse/borgwarehouse-cron.sh
set -euo pipefail
ENDPOINT="$1" # status | storage
URL="http://172.32.91.200:3000/api/v1/cron/${ENDPOINT}"
curl --fail --silent --show-error \
--request POST \
--url "${URL}" \
--header "Authorization: Bearer ${CRONJOB_KEY}"
Create systemd service and timer files.
Alright. Now that the preparations are done, we can create the systemd files.
Let’s begin with the *.service files. I will create two separate services, one for the status and one for storage.
These will go into the /etc/systemd/system/ folder.
$ sudo vim /etc/systemd/system/borgwarehouse-status.service
[Unit]
Description=BorgWarehouse status check
[Service]
Type=oneshot
EnvironmentFile=/etc/borgwarehouse/borgwarehouse-cron.env
ExecStart=/etc/borgwarehouse/borgwarehouse-cron.sh status
$ sudo vim /etc/systemd/system/borgwarehouse-storage.service
[Unit]
Description=BorgWarehouse storage check
[Service]
Type=oneshot
EnvironmentFile=/etc/borgwarehouse/borgwarehouse-cron.env
ExecStart=/etc/borgwarehouse/borgwarehouse-cron.sh storage
Now for the .timer files. These will run the service files every hour. You can change the schedule with the OnCalendar directive. For example, something like this is also possible OnCalendar=Mon-Fri *-*-* 12:00:00
The syntax is as follows.
OnCalendar=<weekday> <year>-<month>-<day> <hour>:<minute>:<second>
These shortcuts are also possible.
OnCalendar=minutely, hourly, daily, monthly, weekly, yearly, quarterly, semiannually
$ sudo vim /etc/systemd/system/borgwarehouse-status.timer
[Unit]
Description=Run BorgWarehouse status check hourly
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
$ sudo vim /etc/systemd/system/borgwarehouse-storage.timer
[Unit]
Description=Run BorgWarehouse storage check hourly
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
Alright. We are done. Re-read the systemd daemons and enable the timers. Don’t use enable on the services, since these don’t have the [Install] section and won’t work with enable.
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now borgwarehouse-status.timer ; sudo systemctl enable --now borgwarehouse-storage.timer
If you want to test the service immediately, just execute the systemctl start command on the service.
$ sudo systemctl start borgwarehouse-status.service
And that’s it.
Hope this helps. Till next time.
Comments