Tim Allen

Software Reverse Engineer

Backup scripts I have known

I need to recreate the mac backup script that rsyncs the sparse bundle (timemachine) up to a server. /usr/bin/env bash

Since we're talking bash, YSAP!

current but simple

!/bin/sh

backuphost="tallen@home.integsys.biz"
backupdir="../Backups/www.integsys.biz"
date=`date "+%Y-%m-%dT%H:%M:%S"`

nice -19 rsync -azPq \
        --exclude /boot \
        --exclude /proc \
        --exclude /sys \
        --exclude /media \
        --exclude /dev \
        --exclude /usr \
        --exclude /var/lock \
        --exclude /var/run \
        --link-dest=../current \
        / \
        $backuphost:$backupdir/back-$date

ssh $backuphost "rm -f $backupdir/current && ln -s back-$date $backupdir/current"

#ssh $backuphost "find Backups/integphone_server1 -maxdepth 1 -mtime +10 -not -name \"*T23:55:00*\" -exec rm -rf {} \;"

Local Backup

This runs a local backup to a mounted file system (ext4 or btrfs). It does de-duplication at the file level; that is, the first backup copies everything, subsequent backups only copy the changed files and hard link to the unchanged files. RAID storage systems like NetApp de-duplicate at the block level but the snapshot paradigm is very similar.

#!/bin/bash

hostname=`hostname`
now=`date "+%Y-%m-%dT%H:%M:%S"`

echo -e "\n\nBackup of $hostname on `date`"

#check args, set backupdir
if [ $# -lt 1 ]; then
        echo "$0: missing path operand"
        echo "Usage: $0 DEST"
        exit 1;
fi
backupdir=$1

#make sure the destination dir is there
if [ ! -d $backupdir/$hostname ]; then
        mkdir $backupdir/$hostname
fi

#run the backup
rsync -aHA --stats --delete \
--exclude /home \
--exclude /boot \
--exclude /proc \
--exclude /sys \
--exclude /media \
--exclude /dev \
--exclude /tmp \
--exclude /run \
--exclude /mnt \
--exclude /exports \
--exclude /var/lock \
--exclude /var/run \
--exclude='ExcludeFromBackup' \
--link-dest=../current \
/ \
$backupdir/$hostname/back-$now

#update the "current" soft link
rm $backupdir/$hostname/current
ln -s back-$now $backupdir/$hostname/current

#prune backups
#After ten days we only keep weekly
for daysago in `seq 11 90`;
do
        date=`date --date="$daysago days ago" "+%Y-%m-%d"`
        dow=`date --date="$date" "+%a"`
        eom=`date --date="$[daysago-1] days ago" "+%-d"` #1 if it's the end of the month
        if [ "$dow" == "Fri" ]; then
                continue; fi
        if [ "$eom" == "1" ]; then
                continue; fi

        rm -rf $backupdir/$hostname/back-$date*
done

#After 90 days we only keep monthly
for daysago in `seq 91 365`;
do
        date=`date --date="$daysago days ago" "+%Y-%m-%d"`
        eom=`date --date="$[daysago-1] days ago" "+%-d"` #1 if it's the end of the month
        if [ "$eom" == "1" ]; then
                continue; fi

        rm -rf $backupdir/$hostname/back-$date*
done

Remote Backup

This runs a remote backup via ssh to a mounted file system (ext4 or btrfs). It does de-duplication at the file level; that is, the first backup copies everything, subsequent backups only copy the changed files and hard link to the unchanged files. RAID storage systems like NetApp de-duplicate at the block level but the snapshot paradigm is very similar.

#!/bin/bash

hostname="kmac"
now=`date "+%Y-%m-%dT%H:%M:%S"`

echo -e "\n\nBackup of $hostname on `date`"

#check args, set backupdir
if [ $# -lt 1 ]; then
        echo "$0: missing path operand"
        echo "Usage: $0 DEST"
        exit 1;
fi
backupdir=$1

#make sure the destination dir is there
if [ ! -d $backupdir/$hostname ]; then
        mkdir $backupdir/$hostname
fi

#run the backup
rsync -aHA --stats --delete \
--rsh 'ssh -c arcfour128' \
--exclude /boot \
--exclude /proc \
--exclude /sys \
--exclude /srv/ProgrammersFiles \
--exclude /media \
--exclude /dev \
--exclude /tmp \
--exclude /run \
--exclude /mnt \
--exclude /opt \
--exclude /exports \
--exclude /var/lock \
--exclude /var/run \
--exclude='ExcludeFromBackup' \
--link-dest=../current \
$hostname:/ \
$backupdir/$hostname/back-$now

#update the "current" soft link
rm $backupdir/$hostname/current
ln -s back-$now $backupdir/$hostname/current

#prune backups
#After ten days we only keep weekly
for daysago in `seq 11 90`;
do
        date=`date --date="$daysago days ago" "+%Y-%m-%d"`
        dow=`date --date="$date" "+%a"`
        eom=`date --date="$[daysago-1] days ago" "+%-d"` #1 if it's the end of the month
        if [ "$dow" == "Fri" ]; then
                continue; fi
        if [ "$eom" == "1" ]; then
                continue; fi

        rm -rf $backupdir/$hostname/back-$date*
done

#After 90 days we only keep monthly
for daysago in `seq 91 365`;
do
        date=`date --date="$daysago days ago" "+%Y-%m-%d"`
        eom=`date --date="$[daysago-1] days ago" "+%-d"` #1 if it's the end of the month
        if [ "$eom" == "1" ]; then
                continue; fi

        rm -rf $backupdir/$hostname/back-$date*
done