Incremental backup script in bash

The previous backup script is a bit limited as it always creates a full backup. In that post I already stated we should combine it with an incremental backup which only back-ups the changed files.

This post shows an incremental backup script in bash.

The script

#!/bin/bash
trgdir="/data/backup/xxx/"
date=$(date "+%Y-%m-%d %H;%M")
fn="xxx $date.tar"
tar czvf "$trgdir$fn" find /data/xxx -mtime -1 -type f
gzip "$trgdir$fn"

Save the script and store it wherever you want. You can add it to your crontab (crontab -e) to run on a schedule.

Explanation

The part that takes care of the “incremental” feature is:

find /data/xxx -mtime -1 -type f

The find command makes it possible to find objects in the filesystem. The first parameter is the map which should be searched. The following parameters are:

-mtime
targets: “last modified” date
value: changes in X * 24 hours ago

-type
targets: object type
value: only select files (“f”)

The result of this command is a list of filenames (with its path) of changes files in the selected directory. All files in the list will be backupped by this script.

For more information about the find command, see: manpage find