#!/bin/bash cd "${0%/*}" || exit 1 ## get SSH_AUTH_SOCK if [ -z "$SSH_AUTH_SOCK" ]; then for p in /tmp/ssh-*/agent.*; do if [ -e "$p" ] && [ -O "$p" ] && ps -p "${p##*.}" &> /dev/null then SSH_AUTH_SOCK="$p" export SSH_AUTH_SOCK break fi done fi ## backups stored in directory_where_this_script_is/host/date_in_year_month_day ## only the last 15 days are stored, and the last 2 are used with ## --link-dest of rsync (ie., check those directories and hard link if ## match found instead of downloading) ## backup function ## bck [ .... ] bck() { host="$1" from="$2" shift 2 ## check arguments: won't accept spaces if [ -z "${host%%* *}" -o -z "${from%%* *}" -o -z "${PWD%%* *}" ] then echo "arguments or backup directory contain spaces" 1>&2 echo "this is not supported" 1>&2 exit 1 fi ## reset arguments arg= ## verbose, if writing to a tty tty -s && arg="-vP" ## current date in year-month-day now=`date -I` mkdir -p $host/$now ## n of current backups count=`ls $host/|wc -l` ## reset link-dest arguments link= ## check previous backups for d in `ls $host/`; do ## skip if if directory is today's backup [ $d = $now ] && continue if [ $count -gt 15 ]; then ## remove if more than 15 rm -fr $host/$d elif [ $count -le 2 ]; then ## add to link-dest if lower/eq than 2 link="--link-dest=$PWD/$host/$d $link" fi ((count--)) done ## do the backup rsync -aH --delete $arg $link "$@" $from/ $PWD/$host/$now/ } bck server1 server1:/home/user \ --exclude /downloads/ \ --exclude /.ccache/ \ --exclude /.beagle/ \ --exclude /tmp/ \ --exclude /rpms/ \ --exclude .hc bck this /home/user \ --exclude /.ccache/ \ --exclude /.beagle/ \ --exclude /downloads/ \ --exclude tmp \ --exclude rpms \ --exclude .hc \ --exclude .bc