How to Auto Backup Files to USB Media When Connected

A backup is the last defense against data loss, offering a means to restore original data. You can use either a removable media such as an external hard drive or USB flash disk or...

A backup is the last defense against data loss, offering a means to restore original data. You can use either a removable media such as an external hard drive or USB flash disk or a shared network folder, or a remote host to back up your data. It’s very easy (and equally essential) to automatically backup your important files without you having to remember to do so.

Read Also: 24 Outstanding Backup Tools for Linux Systems in 2018

In this article, we will learn how to auto backup data to a removable media after connecting it to your Linux machine. We will test with an external disk. This is a basic guide to get you started with using udev for real-life solutions.

For the purpose of this article, we need a modern Linux system with:

  1. systemd systems and services manager
  2. udev device manager
  3. rsync backup tool

How to Configuring Udev Rules for Removable Media


Udev is a device manager that enables you to define rules that can among others, trigger execution of a program or script when a device is added to or removed from a running system, as part of the device event handling. We can use this feature to execute a backup script after adding a removable media to the running system.

Before we configure the actual rule for device event handling, we need to provide udev some attributes of the removable media that will be used for the backup. Connect the external disk to the running system and run the following lsusb command to identify its vendor and product ID.

For the testing purpose, we will be using 1TB external hard disk as shown.

$ lsusb
Find Device Vendor ID of Removable Media

Find Device Vendor ID of Removable Media

From the output of the above command, our device vendor ID is 125f, which we will specify in the udev rules as explained below.

First remove the connected media from the system and create a new udev rules file called 10.autobackup.rules under the directory /etc/udev/rules.d/.

The 10 in the filename specifies the order of rules execution. The order in which rules are parsed is important; you should always create custom rules to be parsed before the defaults.

$ sudo vim /etc/udev/rules.d/10.autobackup.rules

Then add the following rule in it:

SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="125f" SYMLINK+="external%n", RUN+="/bin/backup.sh"

Let’s briefly explain the above rule:

  • "==": is an operator to compare for equality.
  • "+=": is an operator to add the value to a key that holds a list of entries.
  • SUBSYSTEM: matches the subsystem of the event device.
  • ACTION: matches the name of the event action.
  • ATTRS{idVendor}: matches sysfs attribute values of the event device, which is the device vendor ID.
  • RUN: specifies a program or script to execute as part of the event handling.

Save the file and close it.

Create a Auto Backup Script

Now create a auto backup script that will auto backup files to removable USB when connected to the system.

$ sudo vim /bin/autobackup.sh 

Now copy and paste the following script, make sure to replace values of BACKUP_SOURCE, BACKUP_DEVICE and MOUNT_POINT in the script.

#!/usr/bin/bash
BACKUP_SOURCE="/home/admin/important"
BACKUP_DEVICE="/dev/external1"
MOUNT_POINT="/mnt/external" #check if mount point directory exists, if not create it
if [ ! -d “MOUNT_POINT” ] ; then /bin/mkdir “$MOUNT_POINT”; fi /bin/mount -t auto “$BACKUP_DEVICE” “$MOUNT_POINT” #run a differential backup of files
/usr/bin/rsync -auz "$MOUNT_LOC" "$BACKUP_SOURCE" && /bin/umount "$BACKUP_DEVICE"
exit

Then make the script executable with the following command.

$ sudo chmod +x /bin/autobackup.sh

Next, reload udev rules using following command.

$ udevadm control --reload

The next time you connect your external hard disk or whatever device you configured to the system, all your documents from the specified location should be auto backed up to it.

Note: How effectively this works may be influenced by the filesystem on your removable media and the udev rules you write, especially capturing the device attributes.

For more information, see the udev, mount and rsync man pages.

$ man udev
$ man mount $ man rsync 

You might also like to read these following Linux backup related articles.

  1. rdiff-backup – A Remote Incremental Backup Tool for Linux
  2. Tomb – A File Encryption and Personal Backup Tool for Linux
  3. System Tar and Restore – A Versatile Backup Script for Linux
  4. How to Create Bandwidth-efficient Backups Using Duplicity in Linux
  5. Rsnapshot – A Local/Remote Backup Tool for Linux
  6. How to Sync Two Apache Web Servers/Websites Using Rsync

That’s all for now! In this article, we have explained how to auto backup data to a removable media after connecting it to your Linux machine. We would like to hear from you via the feedback form below.

Posted by Web Monkey