One thing I have always struggled with is finding the time to make backups.
You don’t always want to automate this over the internet for various reasons and might want to backup to an external drive that you can stash away safely.
Recently I decided that my home git server should be backed up and stored elsewhere, off-site.
Making sure to do that regularly can be an effort, particularly so, when you run a headless server. Sure, its as easy as turning on a nearby PC, SSH in and run the backups, but I’m lazy, so there has to be an easier way.
What I’d really like is to plug in a specific USB storage device that I’ve allocated to backups, and when I do so, it will trigger backups automatically and better yet, let me know somehow when it is done, after all, this is a headless server (meaning it has no monitor, or a keyboard and mouse connected).
The answer, in this case is; “udev”. Note, it’s not the only answer, “systemd” is the new kid on the block and perhaps later I’ll update with a systemd implementation.
What I have put together is a way to launch a script when the serial number of a specific USB drive is plugged in and detected. For this, I use “udev” (documentation). “udev” is a dynamic device management tool that allows you to perform actions on device events.
In this case, the “rule”, which is what we call the configuration file that performs the detection waits for a device of the USB kind, checks for a serial number that matches one we set (well find this using some other tools later) and then performs an action we tell it to. In this case, we want it to run a script to backup our git repositories.
The rule can be made to run any script you like, it doesn’t necessarily need to be a git backup, nor need it be a backup script at all. It could he used to start a program like a web browser, or a photo editor when you plug in your camera. Simple replace the git backup script later on with your own, and you’re good to go.