Mastering rsync and Bash to Backup Your Linux Desktop or Server

Keeping good backups of your data is important; don’t be the sucker who loses important files and has to deal with it afterwards! In this in-depth guide, you’ll learn about using rsync and lftp to transfer files, writing your own scripts and automating them, and backing up to external storage, a NAS, and a remote server without passwords.

Mounting External Storage Under Linux & Creating Persistent Mounts

On this page, we will be tackling the basics of mounting your external storage device. This could be a flash-based device or an external hard-drive, such as one in an enclosure. If you know the basics of disk mounting, then you can bypass this section entirely.

Note: Is the storage device in question operational once it’s plugged into the PC? You can skip on down to the “Editing fstab for Persistent Mounts” section. If you have the file system support you need but your distro doesn’t automatically mount your storage device, you should head instead to the “Creating a Folder and Mounting External Storage to It” section.

Checking for File System Support: The easiest way to see if your distro currently supports the file system you need is to open up a terminal, enter su (or sudo), and then type in cat /proc/filesystems, as seen below:

Note that certain file systems (eg: exFAT and NTFS with write capabilities) require FUSE (Filesystem in Userspace) support to function (if ‘fuse’ is listed in your /proc/filesystems query as seen above, you’re golden). If FUSE or some other file system isn’t configured for your current kernel, you’ll need to tackle that by recompiling your kernel.

Adding File System Support to Your Kernel: The process of configuring and compiling a kernel under Linux isn’t for the faint of heart, and it’s a subject that would require its own article to explain. If you do need to take this route, and aren’t familiar with updating a kernel, you’ll need to refer to your distro’s documentation.

FUSE can be found under the “File systems” section, typically near the bottom, and is called “FUSE (Filesystem in Userspace) support”. FAT, if it somehow isn’t configured (it’s a kernel default), can be found inside of the “DOS/FAT/NT Filesystems” sub-section.

Creating a Folder and Mounting External Storage to It

Most distros make things easy by automatically mounting your external storage once it’s plugged in, but there’s a potential downside to that: It might create a filepath that differs from time to time (especially if you have two drives plugged in that share a label; eg: “Backups”). Thus, the remainder of this page is dedicated to making sure you’re mounting the drive to the location you want, while also making sure that it’s mounted there every single time you plug it in, regardless of what other storage is connected (even those with a duplicate label).

First things first: Figuring out the drive letter for your external drive. Each storage device plugged in receives a drive letter; IDE will be /dev/hdX; SATA will be /dev/sdX. If your primary hard drive or SSD is of the SATA variety, it will receive the /dev/sda drive-letter. A second similar drive would be /dev/sdb, and so forth. If your system has only two SATA drives installed, and you plug in external storage, it will take on the form of /dev/sdc. To help with the drive letter identification process, a GUI tool like GParted could be called-upon, as it provides important information such as partitions, storage space used, and et cetera.

With this knowledge in hand, mounting a drive should be a simple matter of (assuming it’s a FAT32 drive):

The ‘mkdir’ command creates a folder under /mnt, and while I called mine ‘thumb’, it can be named anything you want; it’s usually best to make it something relevant, however, such as ‘storage’ or ‘exthdd’.

If the external device (or internal, for that matter) happens to be NTFS, it can be mounted using ntfs-3g:

If when attempting to mount using ntfs-3g, it doesn’t work, you’ll need to verify that the required driver is installed. One way to do this is to open up a terminal, enter su (or sudo -s) and then type in ‘mount’ without quotes and then hit . This will show you all of the file systems that your distro supports which aren’t built into the kernel. If ntfs-3g isn’t listed, you will have to search your distro’s repository for it (eg: : Under Gentoo, I had to install it by running emerge ntfs3g as root).

Editing fstab for Persistent Mounts

To make sure a removable storage device mounts to the exact same location each time it’s plugged in, the /etc/fstab file will need to be edited. This is the file that the OS refers to when a new storage device is detected, and without a manual entry, it could be mounted to an unexpected location, rendering a backup script ineffective.

The first step: Establish the storage device’s UUID (unique identifier). After plugging the device in, you can either use GParted’s “Information” option to find the UUID of a partition, or run ls -l /dev/disk/by-uuid/ to find it quicker (by looking for the known /dev/sdX value).

After running that latter command, I could see “6634EE1034EDE2D3 -> ../../sdd1″, which I knew for a fact was the correct drive. So, editing /etc/fstab, I added this line:

good article . you did not explain the terms used here or what these are rsync –av ––delete -e ssh [email protected].1.5:/home/techgage/ /mnt/NAS/Backups/MainPC/techgage/
rsync –av ––delete -e ssh root@targetipaddress:/remotefolder/ /localfolder/

http://techgage.com/ Rob Williams

All of those switches except for –delete are covered in the “Run-down of Common rsync Switches & Processes” section, while –delete is explained under “Removing Old Files from the Target to Keep a Perfect Sync”.

-av: archive mode; includes all folders/sub-folders and files inside them.
-e: calls up an external app, like ssh.
–delete: deletes files on the source that no longer exist on the target; that’d in effect mirror folder A to folder B.