Yet another Rsync backup script (#2)
Posted: Sat Dec 07, 2013 11:56 pm
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:
The backup script is as follows:
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.
Code: Select all
$ sudo mkdir -p /var/backups/rsync/vtapes/slot{1,2,3,4,5,6,7}
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