Linux Commands

How to Auto Backup Files to USB Media When Connected

Backing up the data is critical now a days. The loss of important files due to device faults or inadvertent deletion can be disastrous. One of the most popular Linux distributions, Ubuntu, provides a dependable and secure operating system for a variety of computing applications. If we use Ubuntu, we may be wondering how to automate the process of backing up the files to a USB device whenever it is connected. Fortunately, Ubuntu has a number of tools and techniques to complete this process quickly and easily.

Let us discuss about various methods to automatically backup our files to a USB disc when connected in Ubuntu. We’ll go through the command-line methods for those who prefer the terminal’s power and versatility. By taking these actions, we can ensure that our critical data is safe and accessible, reducing the chance of permanent data loss.

Udev Rules for Removable Media Configuration

Create a new udev rules file called “10.autobackup.rules” in the “/etc/udev/rules.d/” directory and remove the related media from the system.

The sequence in which the rules are parsed is critical; bespoke rules should always be parsed before the default rules.

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

Then, include the following rule:

SUBSYSTEM=="block", ACTION=="add", ATTRS{idVendor}=="125f" SYMLINK+="external%n", RUN+="/bin/autobackup.sh"
  • “==”: It is an equality comparison operator.
  • “+=”: It is an operator that adds a value to a key that stores a list of items.
  • ACTION: It indicates the name of the event action.

Making an Automatic Backup Script

Now, we can make an auto backup script to save the backups to a USB drive when attached to the system.

$ sudo vim /bin/autobackup.sh

Make sure to change the settings for BACKUP_SOURCE, BACKUP_DEVICE, and MOUNT_POINT when we copy and paste the following script:

#!/usr/bin/bash

BACKUP_SOURCE="/home/admin/important"

BACKUP_DEVICE="/dev/external1"

MOUNT_POINT="/mnt/external"

** many more outputs **

We can make it executable by running the following script:

$ sudo chmod +x /bin/autobackup.sh
  • sudo: This commands permits to run any commands as root. We are required to enter our password to validate our authorization when using sudo.
  • +x: When combined with chmod, the “+x” option adds the “execute” (or “x”) permission to the file.
  • /bin/autobackup.sh: This is the path to the script file whose permissions we want to change.
$ udevadm control --reload
  • udevadm: Udevadm is a command-line application that is used in Linux to control and administer the “udev” (device manager) system. Udev is in charge of maintaining the device nodes in the “/dev” directory as well as device events.
  • control: A “udevadm” subcommand used to control many features of the “udev” system.
  • –reload: This is a control subcommand option that asks “udev” to reload its rules and configuration files. When we reload “udev”, it reevaluates the rules and applies any modifications that we made.

When we connect our external hard disc or whatever device we configured to the system again, all of our documents from the selected place should be automatically backed up to it.

We must note that the filesystem on our portable media and the “udev” rules that we create, particularly those capturing the device attributes, may have an impact on how well this works.

$ man udev

$ man mount

$ man rsync
  • man udev: This command displays the “udev” manual page which is the Linux device manager subsystem. The “udev” system manages the device nodes in the “/dev” directory, assigns persistent device identifiers, and manages the device events. The manual page gives a high-level overview of “udev” and its components, as well as an extensive information about its setup, rules, and command-line utilities.
  • man mount: This command displays the “mount” command’s documentation page. It is used to mount the file in Linux. The “mount” command is explained in detail on the manual page, including the many arguments and syntax available. It includes thorough instructions to mount the various file systems, manage the mount points, and control the mount parameters.
  • man rsync: This displays the “rsync” documentation page which is a versatile and commonly used application for file synchronization and transfer. The “rsync” command can synchronize the files and directories between various places, either locally or via a network. The instructional page covers how to use “rsync” and its different options including how to do backups, incremental transfers, and more.

Conclusion

Adding an automated backup system to a USB media provides a dependable and convenient way to protect our crucial files. We may create a smooth backup routine that protects our data by following the procedures that are indicated in this article. Take the proactive action today to avoid the agony of potential data loss and to enjoy the peace of mind that comes with a reliable backup solution.

Similar Posts