Backup files to git repo before shutdown

Publish date: Jul 24, 2020
Categories: linux

I have a few files that I want to push to a remote repository just before shutdown. It could be many use cases, but mine is, I want my dotfiles synced to mbtamuli/dotfiles.

Well, the approach is simple

Create another file and do whatever you need /usr/local/bin/backup_work.sh

#!/bin/bash

cd /path/to/git/repo
git add .
git commit -m "Daily Backup"
git push origin master

Add executable permissions

chmod +x /usr/local/bin/backup_work.sh

Create a file at /etc/systemd/system/backup-work.service. Change the user and group!

[Unit]
Description=Backup files to git repo
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
User=mbtamuli
Group=mbtamuli
ExecStart=/usr/local/bin/backup_work.sh

[Install]
WantedBy=halt.target reboot.target shutdown.target