Page 1 of 1

Yet another Rsync backup script (#2)

Posted: Sat Dec 07, 2013 11:56 pm
by ^rooker
A few years ago, I've already posted a script which backups all folders exported by rsync.

That's nice and everything, but here's another rsync backup script we're using on one of our servers.
It's quite straightforward and incorporates a x-days rollback backup.

Hard facts:
  • Uses hardlinks to save space. A lot.
  • We use a "7days mapping": slot1=monday, slot7=sunday.
  • The code is very straightforward.
Create the backup "slot" folders in "/var/backups/rsync/vtapes":

Code: Select all

$ sudo mkdir -p /var/backups/rsync/vtapes/slot{1,2,3,4,5,6,7}
The backup script is as follows:

Code: Select all

#!/bin/bash
# @date: 26. JAN. 2013
# @author: ^Rooker
# @description:
#   Just a thin wrapper around RSYNC for backup purposes to centralize
#   its commandline arguments in one place.
#   To be called by cron.

RSYNC="/usr/bin/rsync"
RSYNC_ARGS="-avP --delete"
BACKUP_MASK="/var/backups/rsync/vtapes/slot%d"

# Folders to backup:
FOLDERS="/etc /var/www /var/backups /opt /home"

 
# --------------
SLOT="$1"                                               # Current backup slot number is provided as arg#1
BACKUP_DIR=$(printf "$BACKUP_MASK" $SLOT)               # Current backup folder
BACKUP_DIR_PRE=$(printf "$BACKUP_MASK" $((SLOT -1)))    # Previous backup folder (for hardlink stuff)

LOGFILE="$BACKUP_DIR/backup.log"
TIMESTAMP=$(date)

if [ -d "$BACKUP_DIR" ]; then
    touch "$BACKUP_DIR"                     # Update timestamp on folder to easily see when last backup has run
    printf "===================\n$TIMESTAMP\n===================\n\n" > "$LOGFILE"

    # Copy the previous backup as hardlink (to save space and speed things up):
    # (if the previous backup exists)
    if [ -d "$BACKUP_DIR_PRE" ]; then
        echo "Hardlinking previous backup ($BACKUP_DIR_PRE) to slot #$SLOT..." #DELME
        cp -al $BACKUP_DIR_PRE/* $BACKUP_DIR/
    else
        echo "No previous backup slot in '$BACKUP_DIR_PRE'."
    fi

    for SOURCE_DIR in $FOLDERS; do
        TARGET_DIR="${BACKUP_DIR}${SOURCE_DIR}"
        mkdir -p "$TARGET_DIR"

        #CMD="$RSYNC $RSYNC_ARGS --log-file \"$LOGFILE\" --log-file-format \"%t\n\" \"$SOURCE_DIR/\" \"$TARGET_DIR\""
        CMD="$RSYNC $RSYNC_ARGS --log-file \"$LOGFILE\" \"$SOURCE_DIR/\" \"$TARGET_DIR\""
        echo "$CMD"

        echo "" >> "$LOGFILE"
        echo "------------------------" >> "$LOGFILE"
        echo "$CMD" >> "$LOGFILE"
        echo "========================" >> "$LOGFILE"
        echo ""

        eval "$CMD"
    done
else
    echo ""
    echo "ERROR: Backup folder '$BACKUP_DIR' does not exist."
    echo ""
fi

Rollback based rsync backup: Crontab

Posted: Sun Dec 08, 2013 12:07 am
by ^rooker
For a 7-days-a-week backup, just add the following lines to your /etc/crontab:

Code: Select all

0 5     * * 1   root    /etc/homegrown/backup_rsync.sh 1
0 5     * * 2   root    /etc/homegrown/backup_rsync.sh 2
0 5     * * 3   root    /etc/homegrown/backup_rsync.sh 3
0 5     * * 4   root    /etc/homegrown/backup_rsync.sh 4
0 5     * * 5   root    /etc/homegrown/backup_rsync.sh 5
0 5     * * 6   root    /etc/homegrown/backup_rsync.sh 6
0 5     * * 0   root    /etc/homegrown/backup_rsync.sh 7
There's probably a way to summarize this into 1 crontab line...