For about six months I’ve been using a WebDAV share to view and edit OrgMode files on Android and in Emacs (post about it). The folder is included in my daily backup and I haven’t lost any data so far. However, I’d like version control and possibly to run a Forgejo Action on the contents, so the data should be committed to a Forgejo repository.
The first step is to set up Git and pushing to Forgejo on the Raspberry Pi running the WebDAV Docker container. My initial plan was to run a cron job that commits and pushes changes, but systemd can trigger scripts on change, so I started with that.
Git setup
I created a repository named “webdav-backup” on my private Forgejo server. A private GitHub repository should work similarly.
To …
For about six months I’ve been using a WebDAV share to view and edit OrgMode files on Android and in Emacs (post about it). The folder is included in my daily backup and I haven’t lost any data so far. However, I’d like version control and possibly to run a Forgejo Action on the contents, so the data should be committed to a Forgejo repository.
The first step is to set up Git and pushing to Forgejo on the Raspberry Pi running the WebDAV Docker container. My initial plan was to run a cron job that commits and pushes changes, but systemd can trigger scripts on change, so I started with that.
Git setup
I created a repository named “webdav-backup” on my private Forgejo server. A private GitHub repository should work similarly.
To allow the pi user on my Raspberry Pi to push to the repository I created a SSH key with:
ssh-keygen -t ed25519 -C "webdav-backup" -f ~/.ssh/forgejo_deploy_key_webdav
And added the ~/.ssh/forgejo_deploy_key_webdav.pub contents as a Deploy Key to the settings of the repository. It’s important here to enable write access.
To use the key only for this git folder I added this section to my .ssh/config:
Host forgejo-webdav
HostName forgejo.tail07efb.ts.net
User git
IdentityFile ~/.ssh/forgejo_deploy_key_webdav
IdentitiesOnly yes
Then I initialized git in the data folder of my webdav server with:
git init
git switch -c main
git config user.email "webdav@local"
git config user.name "webdav-backup"
# name of the ssh-config entry as host
git remote add origin ssh://git@forgejo-webdav/mfa/webdav-backup.git
Next I commited everything in there as initial version to the repository with:
git add *org
git commit -m "initial"
git push --set-upstream origin main
First part was successful. I have the current webdav contents in the Forgejo repository.
Commit on Webdav update
We want to trigger a commit and push whenever a file in the WebDAV data folder changes. I looked into inotify-tools and watchman, but for now I’ll use only systemd.path. Systemd provides no debouncing, so problems can occur if many changes happen at once.
We need a script that commits the changes. I saved it in the folder where I have the Docker compose file of the webdav server and named it webdav-git-commit.sh:
#!/bin/bash
cd /home/pi/webdav/data
git add -A
git commit -m "Auto-commit: $(date '+%Y-%m-%d %H:%M:%S')" || true
git push
Next we need to automatically start the script when a change happened. We need two files. One that has the path trigger and one the service.
The path trigger /etc/systemd/system/webdav-git-sync.path:
[Unit]
Description=Monitor WebDAV folder for changes
[Path]
PathModified=/home/pi/webdav/data
Unit=webdav-git-sync.service
[Install]
WantedBy=multi-user.target
And the service /etc/systemd/system/webdav-git-sync.service:
[Unit]
Description=Commit WebDAV changes to Git
[Service]
Type=oneshot
User=pi
WorkingDirectory=/home/pi/webdav
ExecStart=/home/pi/webdav/webdav-git-commit.sh
Then we need to activate the trigger with:
sudo systemctl enable --now webdav-git-sync.path
For a single change everything worked as expected. I will monitor this for a while and see if I need to debounce with a lockfile.