Is there a Linux CLI backup tool where it can automatically create a backup if a external HD is plugged in? reddit / selfhosted ---------------------------------------------------------------------- wrhnks · 2023-11-03 20:45 I just want to run a backup automatically if a given device is attached to USB. Does anyone know if it exist or people just build custom shell scripts? ​ Thanks! ​ ---------------------------------------------------------------------- epsiblivion · 2023-11-03 20:53 veeam can do it i think. or just run a cron script to check if folder/file exists before running restic ---------------------------------------------------------------------- miguel_caballero · 2023-11-03 21:06 You can do this using incron to monitor a file/folder and launch your backup script automatically. Piece of cake ---------------------------------------------------------------------- _nix-addict · 2023-11-03 21:23 This sounds very easy to do with bash and cron. This is GPT4 generated for the sake of time, double check that this is your intention and ensure you have rsync installed. ```bash #!/bin/bash # Define the source directory and the backup directory SOURCE_DIR="$HOME/my_drive" USB_DIR="/media/usb" # Get the UUID of the USB device you are expecting TARGET_UUID="your-usb-uuid-here" # Check if the USB device is connected USB_UUID=$(lsblk -no UUID | grep "$TARGET_UUID") if [[ $USB_UUID == $TARGET_UUID ]]; then echo "USB device is connected." else echo "USB device not found." exit 1 fi # Calculate space required for backup and available space on USB SPACE_REQUIRED=$(du -s "$SOURCE_DIR" | cut -f1) SPACE_AVAILABLE=$(df "$USB_DIR" | tail -1 | awk '{print $4}') if [[ $SPACE_AVAILABLE -gt $SPACE_REQUIRED ]]; then echo "Sufficient space available. Starting backup..." rsync -av --delete "$SOURCE_DIR" "$USB_DIR/backup" else echo "Insufficient space on USB for backup." exit 1 fi ``` ---------------------------------------------------------------------- geo38 · 2023-11-03 21:24 incron has been removed from Debian 11. It seems to be no longer supported? The current preferred method appears to be systemd.path ---------------------------------------------------------------------- miguel_caballero · 2023-11-03 21:27 Never heard of it. After a quick browsing, it uses the inotify API so tomato/tomeito :p Thanks for the heads up ---------------------------------------------------------------------- Blutti · 2023-11-03 22:18 You can use udev to run arbitrary commands/scripts when a specific USB device is plugged in. I personally use borg backup for all my backup needs. Example udev script I use to change the owner of a USB zigbee transceiver + create a convenient symlink: cat /etc/udev/rules.d/50-cc2531.rules SUBSYSTEM=="tty", \ ATTRS{idVendor}=="0451", \ ATTRS{idProduct}=="16a8", \ SYMLINK+="cc2531", \ OWNER="homeassist" (You probably want to change it to use `ATTRS{serial}` \+ run a custom script) ---------------------------------------------------------------------- thekrautboy · 2023-11-03 22:29 I have my doubts any of that would work... ---------------------------------------------------------------------- chriscrutch · 2023-11-03 23:03 You could probably write a script to execute your backup command IF a certain directory exists in the filesystem, and run that script every minute or so via a cron job. When you connect your device, assuming it gets the same mount point every time, wait at most a minute for your script to see the "magic" directory now exists, then fire off the command-line backup. ---------------------------------------------------------------------- Minituff · 2023-11-04 02:27 I think you might be able to do this with [Nautical](https://github.com/Minituff/nautical-backup), I think the backup would just fail when the drive is not connected, but will try again on the next scheduled time. If you test it out and that doesn't work then I can add a custom script field that should allow for the logic you need. ---------------------------------------------------------------------- CatoDomine · 2023-11-04 05:10 You should be able to accomplish this with the backup software of your choice using UDEV or systemd to detect the usb insertion or mount and run a script. * [a discussion on ubuntu forums about using systemd for something like this](https://askubuntu.com/questions/25071/how-to-run-a-script- when-a-specific-flash-drive-is-mounted) * [someone on stackexchange talking about their udev solution.](https://unix.stackexchange.com/questions/28548/how-to-run- custom-scripts-upon-usb-device-plug-in) * a utility called [udisks- glue](https://manpages.debian.org/wheezy/udisks-glue/udisks- glue.1.en.html) might be of some use, but it's github doesn't seem to have been touched in like 10 years, so it may not be the best choice. I would search for run script on usb insertion or something like that. ---------------------------------------------------------------------- setipio · 2023-11-04 16:59 Restic is very good...