CIQ

How to Automate Directory Backups in Rocky Linux

How to Automate Directory Backups in Rocky Linux
April 18, 2023

Since the humble beginnings of Linux, one of its strengths has been flexibility. The ability to do things exactly as you want is a part of the ethos of Linux. And it doesn't matter what you're doing with Linux: you can bend it, twist it, and shape it to serve exactly how you want.

One only needs to examine the multitude of methods for backing up directories. You can go with a GUI, a web-based tool, a client/server system, an open source solution or a proprietary one. There's practically no limit to what you do and use for backups in Linux.

We've already covered backing up a directory in Rocky Linux, using the rsync command. That process is quite simple and very handy to have. With a single command you can backup files and directories like a pro. However, the method described in the previous article requires you do this manually. What if you want to automate that process? It should come as no surprise that Rocky Linux has all the tools you need for this, built right in. All you need to do is create a bash script and a cron job and that backup will happen regularly, without you having to run the command.

Let me show you how this is done.

What you'll need

The only things you need are a running instance of Rocky Linux, a local directory to back up, and either an attached external drive to save the backup or a remote Linux server to house the files.

That's it. Let's get to work.

How to create the bash script for the backup

The first thing to do is create a bash script that will be used to run the backup to an external drive. Let's make this very simple. We're going to stick with the same directories that we spoke of in the previous article and will also assume you've successfully mounted an external drive to the /data directory on your Rocky Linux machine. On top of that, we'll assume the directory you want to back up is ~/Documents.

Create the new bash script with the command:

nano ~/backup.sh

In that file, add the following content:

!/bin/bash
rsync -a ~/Documents /data/Documents/

Note: If you're backing up a different directory to a different location, make sure to edit the script above.

Save and close the file.

You must now give the script executable permission with the command:

chmod u+x ~/backup.sh

Before moving on to the automation section, test the backup script with the command:

~/backup.sh

It should go off without a hitch.

Let's make it a bit fancier and automatically create a new directory named with the current date. This way, each daily backup will be housed in its own unique folder. The script for this would be:

!/bin/bash
DATE=$(date +%d-%m-%Y)

rsync -a ~/Documents /data/Documents/$DATE/

Save and close the file.

How to automate the backup

Let's assume you want your backup script to run everyday at midnight. For that, we're going to create a cron job.

Back at the terminal window, open your crontab file for editing with the command:

crontab -e

Scroll to the bottom of that file and add the following line:

00 00 * * * /home/USER/backup.sh

Where USER is your Rocky Linux username. Save and close the crontab file and now, every day at midnight the backup script will run.

How to automate rsync backups over SSH

The first thing you must do is create an SSH key and then copy it to the remote server you wish to house your backups.

Create the SSH key with:

ssh-keygen

Answer the questions when prompted. When asked for a password, give the key a blank password by simply pressing enter. We need to leave the key file’s password blank because we will be using it for automated tasks, and cannot be prompted for a password each time.

Save the key in the default location ( ~/.ssh/ ), and keep it safe! This key file (the one that does NOT end in “.put”) is similar to your password, and should be guarded. Once you've done that, copy the key to the remote server with:

ssh-copy-id SERVER

Where SERVER is the IP address of the remote machine.

With the key copied, test the login with:

ssh SERVER

Where SERVER is the IP address of the remote host.

Note: The above commands assume your remote and local usernames are the same.

You should be prompted for the SSH key password and not your user password. If not, you'll need to enable SSH Key Authentication on your remote machine, which is done in the /etc/ssh/sshd_config. Look for the following line:

PubkeyAuthentication yes

Remove the # character from the line and save the file. Restart SSH with:

sudo systemctl restart sshd

Now, SSH access via key authentication should work.

With that taken care of, our new bash script (we'll call this one backupssh.sh) looks like this:

!/bin/bash

DATE=$(date +%d-%m-%Y)
rsync -a -e ssh ~/Documents USER@SERVER:/data/$DATE

Where USER is your remote username and SERVER is the IP address of the remote server.

Save and close the file.

Give the file executable permissions with the command:

chmod u+x backupssh.sh

Test the file with the command:

./backupssh.sh

The backup should run without a problem.

To automate this script, we do the same thing. Create a new entry in your crontab with:

crontab -e

At the bottom of the file, add the following:

0 0 * * * /home/USER/backupssh.sh

Save and close the file.

One thing to keep in mind is if you run two backups (one to a locally attached drive and one to a remote server), you'll want to create different times for their cron jobs. So one can go off at midnight (0 0 * * *) and one could go off at 12:30 AM (30 0 * * *).

And that's the gist of automating backups in Rocky Linux to both locally attached drives and remote servers. You can modify this setup to perfectly meet your needs…thanks to the power and flexibility of Linux.

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
36
>>>