Score:2

Backup some files and folders to flash in Ubuntu

ng flag

I'm using the following script to backup some files and folders.

#!/bin/sh
#backup.sh  
zip -r backup_`date +%F | sed -e "s/-/_/g"`.zip \
  /home/xralf/.local/share/Anki2/my_deck \
  /home/xralf/.local/share/Anki2/addons \
  /home/xralf/.local/share/Anki2/prefs.db \
  /home/xralf/books \
  /home/xralf/.mozilla \
  /home/xralf/.vim \
  /home/xralf/.vimrc \
  /home/xralf/.vim_bookmarks \
  /home/xralf/.NERDTreeBookmarks \
  /home/xralf/.bashrc \
  /home/xralf/.bash_aliases \
  /home/xralf/.bash_profile \
  /home/xralf/.profile \
  /home/xralf/.Xmodmap \
  /home/xralf/Downloads/sitedelta-backup.json
  /home/xralf/.config/i3/config \
  /usr/share/X11/xkb/symbols/us \

I run the script manually and then copy the files to flash drive.

I'd like to improve the process to be able to backup more often. The goal is to have a backup of these files and folders on the flash drive.

I'd like to only press a button or start a command and it would copy all the selected files and folders to the flash drive, but I'd like to backup everyday only what has changed (syncing, additions/deletions) , so it would be quick. Once in a week (or month) I'd like to create a complete backup with my script backup.sh

What do you recommend (script or application)?

oldfred avatar
cn flag
I prefer to just use rsync. Others suggest rdiff. One copy is not a backup, so I copy to different flash drives, hard drives & manually to DVDs. With rsync, only changes get copied each time. And easy to recovery individual files as not compressed. I started with a bash script and something like this: `rsync -avz /home/fred /mnt/backup/` Then added excludes and only some folders. http://askubuntu.com/questions/545655/backup-your-home-directory-with-rsync-and-skip-useless-folders
j0h avatar
au flag
j0h
So just to clarify, you don't want this to run as a scheduled process, but instead want to click on a button or applet to initiate the process?
Carles Mateo avatar
cn flag
As @oldfred mentions, don't relay in a single device for backups and rsync is the best for what you need. Flash drives die often, I prefer to have backups that keep all the data based on a Timestamp. I have this page that may be helpful: https://blog.carlesmateo.com/2022/10/23/backup-and-restore-your-ubuntu-linux-workstations/
oldfred avatar
cn flag
Nothing preventing you from putting the backup script into cron or now systemD timer. Then it is automatic. https://rdiff-backup.nongnu.org/examples.html
ng flag
@j0h It can be scheduled process, but I have to push the flash memory into the laptop, it's not there all the time. So, when I'm able to push the flash in, I'm able to click on a button as well. But automatization is welcome.
ng flag
@CarlesMateo Thanks. I can use two flash drives.
Score:3
in flag

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:

  1. 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
  1. 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
  1. Enabled your service, so that it starts at boot

sudo systemctl --user enable backup.service

  1. If you want to use it directly, start it:

sudo systemctl --user start backup.service

Edit

  1. Added a line WantedBy in Install section. It is important to create this line before enabling the service.
  2. Also added Type=notify and RemainAfterExit=yesto ensure the service restarts if you take unplug/replug the USB key.
ng flag
Thank you. The line "This is neccessary ... graphical-session.target" is only comment? Could you please more elaborate the "Rsync" comment (example directory and file)?
kanehekili avatar
zw flag
Yes, a # indicates a comment that will **not** be executed. Marc did a good job to explain. `man rsync` will explain how to backup folders. Investigate...
Marc Vanhoomissen avatar
in flag
@xralf: I added a line for the 'rsync' as an example. Now this program has many options and possibilities, including coying on or from other machines. The fact is that it only writes files that changed between 2 executions, meaning the first time will take long but afterwards will be much quicker. Of course, if you instruct it to copy your 2Gb mail file, chances are high that it will have changed.
Artur Meinild avatar
vn flag
Great example of a triggered `systemd` service!
ng flag
Thanks a lot, I will certainly try it and maybe ask something later yet.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.