I suggest you to create a specific service that is to be fired each time you insert your flash drive. The service would trigger a specific shell script to backup what you want and, maybe ,dismount the drive so that its content remains intact. You can also create two different services, one for regular backup, the other one for your weekly backup.
The beauty here is that you even do not need to press a button or manually start a script. It all happens automatically.
More specifically:
- Create the service (a texte file):
/home/ralf/.config/systemd/user/backup.service
(I assume ralf
is your username). It is important to use it as a user specific service so that notification can happen.
This file has the following content:
[Unit]
Description=Backup files to flash drive
Requires=media-ralf-YourFlash.mount
After=media-ralf-YourFlash.mount
[Service]
Type=notify
ExecStart=/home/ralf/backup.sh
Environment="DISPLAY=:0"
RemainAfterExit=yes
Restart=always
RestartSec=60
[Install]
# This is necessary to start the program when the graphical environment is ready, enabling 'notify-send'
WantedBy=graphical-session.target
WantedBy=media-ralf-YourFlash.mount
- Your script
backup.sh
would be something
like:
#!/bin/bash
# Will perform a sync of /home/user/ to flash drive
notify-send "Mounted"
# Rsync
rsync /home/ralf/ /media/ralf/YourFlash/
# Other rsync commands or your zip command.
...
umount /media/ralf/YourFlash
notify-send "Backup complete. Please remove flash drive."
exit 0
- Enabled your service, so that it starts at boot
sudo systemctl --user enable backup.service
- If you want to use it directly, start it:
sudo systemctl --user start backup.service
Edit
- Added a line
WantedBy
in Install
section. It is important to create this line before enabling the service.
- Also added
Type=notify
and RemainAfterExit=yes
to ensure the service restarts if you take unplug/replug the USB key.