gif gif

The interesting corner

gif gif

Replacing my cron backups with SystemD backups

Introduction

Originally, I used cron jobs for all my backups. This works well, but it always kind of feels like throwing the script in a void and hoping it will get executed properly. After setting up gitea and creating the backup script for that, I had a revelation: Why not use SystemD? I'm a big fan of SystemD and I think it works great for service management and controlling when what starts on a system, but since you also have timers, and they come with the amazing OnCalander format, so I could also use it as a replacement for my backup cron jobs.

SystemD as cron jobs

The power of cron is that you can specify, down to the minute, when you want to execute a job. However, this can also be done with SystemD timers. For example, to run a SystemD service named someservice every day at 02:30, you can create the following someservice.timer:

      
[Unit]
Description=Run someservice.service daily at 02:30

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target
      
    

The cool thing with this is that you have all the configuration options SystemD offers, with default logging (if you have it enabled) with JournalD. The persistent=true part is also cool, since that will run the script as soon as possible after boot if it was off during the time it should have run.

I think it's also more user-friendly than cron. While cron als works very well, it can sometimes be confusing to read the schedule from a cron job. For example, running a script every monday at 19:00 looks like this with cron:

      
        19 0 * * 1 /path/to/script.sh
      
    

And with SytstemD, the OnCalander would be this:

      
[Timer]
OnCalendar=Mon *-*-* 19:00:00 # or use Mon,Wed,Fri or Mon..Fri for multiple days
      
    

Makes it a little more convenient to understand when you're revisiting your scripts 6 months after creating them \^o^/

So, after having created the timer and service files to run the scripts, all you have to do to enable them are these 3 commands:

      
sudo systemctl daemon-reload
sudo systemctl enable someservice.timer
sudo systemctl start someservice.timer
      
    

And to check if it is enabled:

  
systemctl list-timers
  
  

Another reason why I like this so much, is the logging and viewing the running backup. Using systemctl status someservice you get the log output, but also which processes have spawned from the service. For example, while my games server was backing up, you can see it was currently backing up the minecraft server:

CGroup: /system.slice/backup-games.service
             ├─375068 /bin/bash /home/sem/backup-scripts/backup-games.sh ">" /home/sem/backup-scripts/backup-games.log
             ├─375106 /bin/bash /home/sem/backup-scripts/backup.sh minecraft
             ├─375107 tee /home/sem/backup-scripts/backup-minecraft.log
             ├─375108 tar czvf minecraft.tar.gz /home/sem/minecraft
             ├─375109 /bin/sh -c gzip
             └─375110 gzip