CIQ

How to Back Up a Directory in Rocky Linux

How to Back Up a Directory in Rocky Linux
March 22, 2023

One of the things we love most about Linux is how flexible it is. Very often, it's possible to get things done with the pre-installed software. And on the occasion you don't have what you need to finish a task, it's often a quick sudo dnf install command away.

One great example of the flexibility found in Linux is when it comes to backing up a directory. This is a task that every admin will come up against at some point. Nearly every server needs backups; it might be a web server, a mail server, or a server with user data. No matter the need, there's a way to back those directories up with ease.

One of the most widely-used backup tools in the Linux community is rsync, which is described (from the man page) as a "fast, versatile, remote (and local) file-copying tool." Rsync is a very easy command line tool to use for backing up directories either to a locally attached drive or even a remote server.

We're going to show you how rsync can be used as a very easy backup tool for Rocky Linux. First, we’ll demonstrate how to use rsync to back up a directory to a locally-attached drive and then do the same thing over your network.

What you'll need

To make this work, you'll need the following things:

  • A running instance of Rocky Linux.

  • An external drive attached to your server.

  • A remote server on your LAN that you can back up to.

  • A user with sudo privileges. 

That's it. Let's create a backup.

How to prepare for your backup

The first thing we're going to do is make sure we have a directory created and our user has proper permissions to use it. Let's say you have an external drive, and it's mounted on the /data directory. Best practice is to find the “UUID” (unique ID) of the external drive’s file system, and add that as an entry into our fstab (File System Tab) file. Finding the UUID is easy: simply run the command blkid .  You’ll see a list of drives and partition types - identify the external drive and its UUID. The UUID will be a long string of characters, something like, for example: 48d9a384-04d0-4987-9da7-2af34ddd54c6 . Copy that string of characters (hyphens included), and let’s edit the fstab file:

sudo nano /etc/fstab

You would then add an entry to mount your device’s UUID to the folder /data. It would look something like this:

UUID=48d9a384-04d0-4987-9da7-2af34ddd54c6    /data xfs   defaults         0   0

Make sure you replace the sample UUID here with your own, or it won’t work!

We mount the filesystem this way instead of the more traditional drive letter (sda1, sdb1, etc.) as a best-practice:  those drive letters may not remain constant every time the system boots, but the UUID will! For example, if you added another external drive to the system, it might be identified as sdb1, where last time it was booted, your original drive was sdb1. We don’t want to cause confusion, so the unique ID is used.

Additionally, we can ensure our fstab mount point works with this simple command:

sudo mount -a

This is the mount all command, it says “read the /etc/fstab file and attempt to mount everything in it”.  We can use it to ensure we didn’t make a mistake while editing the file. When completed, the df -h command should show your external drive mounted under /data.  

You'll also want to make sure your user has write privileges or ownership of the /data directory with a command like this:

sudo chown -R $USER:$USER /data

At this point, your user can work with the rsync command to back up a directory.

How to use rsync to back up to a locally-attached drive

The basic structure of the rsync command is:

rsync OPTIONS SOURCE DESTINATION

Let's backup the ~/Documents directory to /data. You can swap out ~/Documents for any directory you need to back up. You might want to create a Documents directory in data with:

mkdir /data/Documents

The basic backup command for this would be:

rsync -av ~/Documents /data/Documents/

Any file or directory in ~/Documents will be backed up to /data/Documents.

The av options are as follows:

  • a – do a recursive backup to include child directories. This option is different than the standard recursive option (-r), in that syncs recursively while preserving symbolic links, device files, modification times, groups, owners, and permissions. We highly recommend you always use -a over -r for recursive backups.

  • v – show verbose output

One thing to keep in mind is that rsync has a large number of available options. To find out about them, issue the command man rsync and carefully read through the manual page. Just don't get overwhelmed by the number of available options.

How to use rsync to back up to a remote directory

Let's say you have a remote server at 192.168.1.100 with a /data directory configured with permissions that allow your user to access it. With the help of SSH, we can back up the ~/Documents directory with a command like this:

rsync -av -e ssh ~/Documents USER@192.168.1.100:/data

…where USER is a user on the remote machine.

One of the differences, when using SSH with rsync, is that it will automatically create the Documents sub-folder on the remote server (within the /data directory in our case).

When using rsync in conjunction with SSH, you might also want to zip a directory before backing it up. That way, instead of sending a collection of files and folders to the remote backup directory, you're only sending a single file. This can be done in conjunction with the zip command like so:

zip ~/Documents.zip ~/Documents && rsync -av -e ssh ~/Documents.zip USER@192.168.1.62:/data/

...where USER is a remote user. The above command would first create a zip file of ~/Documents, called Documents.zip, and then copy the file to the /data directory on the remote machine.

And that is the easiest method of backing up a directory on Rocky Linux. Next time around, we’ll show you how to automate this process with the help of a bash script and a cron job.

Related posts

2023 Holiday Gift Guide for Rocky Linux Users

2023 Holiday Gift Guide for Rocky Linux Users

Dec 19, 2023

Rocky Linux

Why Rocky Linux Is a Rock-Solid Choice in an Economic Downturn

Why Rocky Linux Is a Rock-Solid Choice in an Economic Downturn

Jan 18, 2023

Rocky Linux

6 Signs That It's Time to Move to Rocky Linux

6 Signs That It's Time to Move to Rocky Linux

Feb 23, 2023

Rocky Linux

123
38
>>>