Skip to content

Instantly share code, notes, and snippets.

@eylenburg
Last active August 3, 2024 14:23
Show Gist options
  • Save eylenburg/1f5735b8612bd4a4b1dff26db9e3755c to your computer and use it in GitHub Desktop.
Save eylenburg/1f5735b8612bd4a4b1dff26db9e3755c to your computer and use it in GitHub Desktop.
SIMPLE scheduled backup of Linux home folder to external HDD or similar

This will check whether your external HDD (or USB stick or SD card) is plugged in and will then backup your /home folder via one-way sync to it as a backup.

What this doesn't do:

  • compress your files
  • deduplicate your files to save storage space

If you want this, you'd better use something like Vorta Backup.

What it does do:

  • mirror your home folder to a backup disk
  • optional: encryption (by simply encrypting the backup disk)
  • optional: versioning of files on the backup disk when they're modified in the home folder

Important warning: If you accidentally delete files and want to restore from your backup, then make sure this script is NOT running before plugging in your backup HDD to restore those files! Otherwise, the script might (depending on your chosen settings) simply delete the files from the backup HDD as it always syncs one-way.

Step 1: encrypt your HDD (optional)

If you want your backed up files to be encrypted (which I would recommend as long as your computer is also encrypted), then you can format your external HDD with LUKS encryption, e.g. in the KDE Partition Manager or GNOME Disks.

You can also give your partition a nice name, in my case I just named it "backup".

Step 2: install FreeFileSync

You might be able to install it from your distro's repo but for this guide I'm using the Flatpak:

Set up Flatpak and Flathub if not already the case.

Then install FreeFileSync: flatpak install --user org.freefilesync.FreeFileSync

Step 3: Create a batch job in FreeFileSync

  1. Left side folder: add source path: /home/user (no trailing slash, replace "user" with your username)

  2. Right side folder: add destination path: /run/media/user/backup/freefilesync (no trailing slash, path to a newly created folder on the backup disk; in my case "backup" is the name of the disk and "freefilesync" is the new folder I created where the backed up files should go)

image

  1. Click on the filter icon in the middle and exlude these folders (relative to source path which in this example is your home folder, and with trailing slash):
/Downloads/
/.local/share/Cryptomator/mnt/
/.local/share/Trash/
/.local/share/containers/
/.dropbox-dist/
/.dropbox/
/.cache/
*.qcow2
*.socket
/.var/app/website.i2pd.i2pd/

These are just examples for some locations I either don't want to back up (like the Downloads folder, Trash, .cache and virtual machine disks) or that will give you error messages (e.g. .dropbox-dist is not accessible for the user). Feel free to amend and add more folders.

image

  1. Back in the main windows, click on the big cogwheel on the top right next to the "Synchronize" button and select "Mirror" to have one-way sync from the left to right folder.

  2. Also select in Delete & Overwrite: "Permanent" (as external disk doesn't support recycle bin). You can also choose versioning if you like but I haven't tried it.

image

  1. Back in the main window, you'll see three save buttons on the top left, click the rightmost one which says "save sync as batch job" and then save the BatchRun.ffs_batch file in the root directory of the backup drive (so in my case it's saved in /run/media/user/backup/BatchRun.ffs_batch).

image

Step 4: Create a monitoring script that runs the backup when the backup disk is mounted

  1. Create a new text file:
#!/bin/bash								
file="/run/media/user/backup/BatchRun.ffs_batch" # location of your FreeFilSync batch job								
while true; do								
    if [ -f "$file" ]; then								
        /usr/bin/flatpak run --command=FreeFileSync --file-forwarding org.freefilesync.FreeFileSync "$file" # amend this command if not using the Flatpak version of FreeFileSync								
        sleep 7200 # 2 hours								
    else								
        sleep 600  # 10 minutes								
    fi								

Make sure to insert the correct location of the batch job for the file variable.

You can also change the times: In this example the script checks if the backup disk (e.g. external HDD) is mounted every 10 minutes and if it is mounted it then runs a new backup every 2 hours.

  1. then save this file, e.g. as freefilesync_start.sh in ~/bin.

  2. Finally, make it executable. In KDE Plasma, right-click on it -> Properties -> Permissions -> [x] Is executable. In the terminal: chmod +x freefilesync_start.sh

Step 5: Put the script in the autostart

KDE Settings -> Autostart -> + Add... -> + Add Login Script -> select the script (e.g. freefilesync_start.sh).

And, in the words of a certain Red Hat developer: I have no idea what Gnome is or does sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment