Keep Your /home Safe With Cron Backups

Objective

Create a backup script that regularly backs up important files from your /home directory.

Distributions

This will work with any Linux distribution.

Requirements

A working Linux install with root access.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

Sure, there are backup utilities for Linux, but with a couple of scripts, you can keep your important files backed up regularly with little to no effort.

A very simple Bash script will do the bulk of the work. For the rest, you’ll take advantage of the cron system already on your computer.

Create The Script

You might have guessed already, but the script that you’ll be writing uses tar. tar will compress your files into a more manageable and space-saving format.

Start off by creating the script. You can put it anywhere, so your /home directory will do fine.

$ vim home-backup.sh
#! /bin/bash

#
# Home Directory Backup Script
#


Now, you can start to construct the script itself. Chances are, you don’t want to back up everything in the directory. There’s probably way too much there, and backing up things like games isn’t usually the best use of system resources.

Certainly, you can, and in that case, ignore the whole directory selection process, and just use /home/user.

So, create a tar line that compresses key directories in your /home.

tar -cJpf /media/backup/home-backup-`date +%d-%m-%Y`.tar.xz /home/user/{Documents,Downloads,Pictures,Music,.config,.Xresources,.xinitrc,.i3,.mozilla,.zshrc}

With just that line, your computer will create a tar archive at /media/backup in a file called home-backup with the current date tagged on. IT will add xz compression and preserve folder structure and permissions as well.

Incremental Backups

While that’s good, it can be better. tar offers incremental backups. Incremental backups will make one huge backup the first time and detect and backup changes for each subsequent run in a separate smaller archive. This way, your computer saves time and space by not backing up the same things repeatedly.

This is what that same command looks like running incrementally.

tar --listed-incremental=/media/backup/snapshot.file -cJpf /media/backup/home-backup-`date +%d-%m-%Y`.tar.xz /home/user/{Documents,Downloads,Pictures,Music,.config,.Xresources,.xinitrc,.i3,.mozilla,.zshrc}

The incremental backup needs a snapshot file to track files and changes. It’s probably a good idea to keep it in the same directory as the backups.

Add It To Crontab

By now, you should have a script that looks something like this.

#! /bin/bash

#
# Home Directory Backup Script
#
tar --listed-incremental=/media/backup/snapshot.file -cJpf /media/backup/home-backup-`date +%d-%m-%Y`.tar.xz /home/user/{Documents,Downloads,Pictures,Music,.config,.Xresources,.xinitrc,.i3,.mozilla,.zshrc}

Be sure to make the script executable.

$ chmod +X /home/user/home-backup.sh

If you’re happy with that, you can move on to creating the crontablinux command as root.

# crontab -u username -e

An editor(probably nano) will open up for you to add your line. When you’re done, it’ll look something like this one.

0 3 * * * /home/user/home-backup.sh

Timing

Alright, the thing that’s probably confusing you is the numbers and asterisks at the beginning of the line. They determine the timing.

The first digit controls seconds. The second is hours. The next two are days and months. The last one is day of the week(1-7). If the place is occupied by an asterisk, the script will run every time.

A number means that the script will run at that time. To use the previous example, that will run the script at exactly 3AM every day. If you wanted to run it at 3:30AM every time instead, it would look like this.

30 3 * * * /home/user/home-backup.sh

When you have a time that works for you, save and exit. The script will automatically run at the time that you specified from now on.

Closing Thoughts

Obviously, you can get add varying layers of complexity here, including encrypted directories and/or directories. You can choose to save multiple days worth of backups and delete older ones automatically too. Of course, it’d be a good idea to include a backup to a remote or networked drive too.

It might be a good idea to write a script that uses tar’s --concatenate flag to merge the archives every so often. Otherwise, you’re going to have loads of archives with very few files in them.

Since this is a scripted method, you’re really only limited by your scripting ability with Bash.



Comments and Discussions
Linux Forum