bootiack ([info]bootiack) wrote,
@ 2008-01-08 16:56:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:rsynch and bash to do incremenatal backu

rsynch and bash to do incremenatal backups
#!/bin/bash

# this script is designed to back up some directories to an external usb-hd via rsync.
# Also there's some magic happening which causes the backups to be saved incrementally.

# meant for directories without trailing slash!... think about it before putting files here!
ToBackup=( /root /boot /etc /home /usr/local )

BACKUP_DIR="/mnt/backup/snapshots"


# TODO: get latest snapshot automagically? (like search BACKUP_DIR for dirs, sort them alphabetically, take last?
LAST_SNAPSHOT="${1##*/}" # FIXME: nasty takes last backup-dir as argument
CURRENT_SNAPSHOT=$(date "+%F %R")


RSYNC_OPTS="-a " # archive mode; same as -rlptgoD (no -H, -A)
# means: r: recursive; l: cp symlinks as symlinks; ptgo: preserve permissions, times, group, owner;
# D: preserve device and special files; (not (H: preserve hard links; A: preserve ACLs))
#RSYNC_OPTS+="-z " # compress data during transfer
RSYNC_OPTS+="-v " # tell us what you're doing
RSYNC_OPTS+="-P " # same as --partial: keep partially transferred files and: --progress: shows progress during transfer
RSYNC_OPTS+="-u " # skip files that are newer on the receiver
RSYNC_OPTS+="-h " # human readable sizes (-h: basis 1000; -hh: basis 1024)
##RSYNC_OPTS+="--delete " # delete extraneous files from dest dirs
# not needed with --link-dest, as newly deleted files are not copied and not linked to old files (only when using cp -al approach)
##RSYNC_OPTS+="--delete-excluded " # also delete excluded files from dest dirs #same as above
#RSYNC_OPTS+="--exclude=$EXCLUDEPATTERNS " # exclude files matching PATTERN
RSYNC_OPTS+="--numeric-ids " # don't map uid/gid values by user/group names
#RSYNC_OPTS+="--bwlimit=KBPS" # average transfer speed limit in KB/S
##RSYNC_OPTS+="--link-dest='../$LAST_SNAPSHOT'" # hardlink to files in DIR when unchanged # DON'T UNQUOTE THIS, see below

for i in "${ToBackup[@]}"
do
echo "Backing up $i ..."
PARENTDIR="${i%/*}" # strips last directory
LINKPATH="$BACKUP_DIR/$LAST_SNAPSHOT/$PARENTDIR"
BACKUPTARGET="$BACKUP_DIR/$CURRENT_SNAPSHOT/$PARENTDIR"

if [[ ! -e "$BACKUPTARGET" ]] ; then # if the backup directory's parents don't exist: create them
echo "mkdir -p $BACKUPTARGET"
mkdir -p "$BACKUPTARGET"
fi

echo "Hardlinking against $LINKPATH"
rsync $RSYNC_OPTS --link-dest="$LINKPATH" "$i" "$BACKUPTARGET"
echo -e "done with $i.\n\n"
done




Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…