This is a simple script which automatically backups files modified in the last n days on a usb stick. It can be run from cron and will require the usb stick to be present only at backup times:

<pre lang="bash">#!/bin/bash

USB_DEV=/dev/sdb1
MOUNT_POINT=/media/backupusb
DAYS=60
SYNC_DIR=/home/tomcat/backup/

find $SYNC_DIR -mtime +$DAYS | sed "s|$SYNC_DIR||" > $SYNC_DIR/exclude

mount $USB_DEV $MOUNT_POINT

if [ $? -eq 0 ]; then
    mkdir -p $MOUNT_POINT/backup
    rsync -rtDxL --safe-links --delete --exclude-from=$SYNC_DIR/exclude --delete-excluded --modify-window=1 -vP $SYNC_DIR $MOUNT_POINT/backup/
    umount $MOUNT_POINT
else
    echo Could not mount $USB_DEV on $MOUNT_POINT
fi

Some explanation on the rsync params:

–modify-window=1 this is required on vfat systems which have a time resolution of 2s. If you don’t use this it will copy the files again and again.

–exclude-from uses the result of the find command to exclude old files

–delete-excluded removes old backup files