Tuesday, October 30, 2007

Poor Man's TimeMachine: rsync

The shell script on the computer being backed up, needs a no-password ssh key to work.

#!/bin/bash

BACKUP_HOST=user@host
SSH_KEY=/home/user/.ssh/host_rsa

LOG_DIR=~/logs
LOG_FILE=$LOG_DIR/backup.`date +%Y%m%d_%H%M`.log

SRC_DIR=/home/user/
DEST_BASE_DIR=/home/user/backups/localhost
DEST_DIR_NAME=backup_`date +%Y.%m.%d_%H.%M.%S`
PREV_DIR_NAME=previous

DEST_DIR=$DEST_BASE_DIR/$DEST_DIR_NAME
PREV_DIR=$DEST_BASE_DIR/$PREV_DIR_NAME

mkdir -p $LOG_DIR

nice -10 /usr/bin/rsync --rsh="ssh -i $SSH_KEY" \
--checksum --archive --compress --human-readable --progress --recursive \
--link-dest=$PREV_DIR \
--filter='merge /home/user/backup/backup.filterrules' $SRC_DIR \
$BACKUP_HOST:$DEST_DIR >> $LOG_FILE 2>&1

ssh -i $SSH_KEY $BACKUP_HOST "link $PREV_DIR $DEST_DIR" >> $LOG_FILE 2>&1

bzip2 $LOG_FILE


A Ruby script that I use with the password-less ssh key on the server side to reduce chances of it being abused. Just add command="~/bin/backup-wrapper.rb" before your key in .ssh/authorized_keys



I'm sure there is a way to do what the wrapper does in bash but I happen to be more comfortable with ruby doing the string parsing. If anyone wants to provide a bash script for doing the same thing I'd be more than happy to see it.

No comments: