Configuring Backup and Shutdown on Connection of a USB Drive in Ubuntu 24.04 LTS
Mike Young Mike Young
11 subscribers
40 views
1

 Published On Sep 27, 2024

This video details setting-up a way to have data backup and shutdown commence upon plugging-in of a specially designated USB storage device on Ubuntu 24.04 LTS, though it would easily work on other distros. I tasked ChatGPT in writing a C program to monitor for a designated drive, identifier specified in a text file, to appear in /media/user. When detected, it launches a backup bash script to backup using rsync, then pops-up a Zenity dialog window of the status, allows the user to abort shutting down, and shuts down in 10 seconds if OK is clicked or no reply in 10 seconds, assuming the backup succeeded.

The source code for the C program, bash backup script, USB identifier text file, and readme.txt instructions can be found at:
https://www.mikesshorts.com/misc/Ubun...

An updated version of this was made with better version of the backup.sh script, a bit better comments, and a fix to support drive IDs with spaces. One doesn't need to specify the username or drive ID in the updated backup.sh script -- just specify that in the usbid.txt file. The updated version (v2) is here:
https://www.mikesshorts.com/misc/Ubun...

I also made a third version that prompts the user when the USB drive is detected and allows them to cancel backing-up and shutting down. That just entailed changing the backup.sh script. Version 3 is here:
https://www.mikesshorts.com/misc/Ubun...

Also, make sure your USB storage drive is formatted as EXT4 or NTFS to ensure that symlinks are backed-up correctly using the rsync command or your backup may fail. In this example, I used NTFS for compatibility with Windows PCs. If you’re using a file system on your backup drive that doesn’t support symlinks then you can add --no-links to your rsync command in the backup.sh script. Just make sure the resulting backup process is backing-up everything you really need.

There might be more clever ways of achieving this end, but I wanted it to be totally plug and play, and it was a fun mini project to continue to be impressed with what ChatGPT is capable of with some prompting and a little bit of basic C programming and bash scripting skills.

show more

Share/Embed