#!/bin/sh # Backup script in less than 50 lines _dir="/backups/servers" hostname="" ssh_opts="" rsync_opts="" log=0 _pth="" _exc="exclude" logdir="/var/log/backups" log_fmt="+%m.%d.%y_.%H.%M.%S" if [ -z $_dir ] ; then echo "You need to setup a base backup directory" fi while [ $# -gt 0 ] ; do case $1 in -h) hostname="$2"; shift; shift; ;; -n) rsync_opts="${rsync_opts}n"; shift; ;; -l) log=1; shift; ;; -lt) log=2; shift; ;; -e) _exc="$2"; shift; shift; ;; -p) _pth="$2"; shift; shift; ;; *) echo "Unknown option: $1"; exit 1; shift; ;; esac done if [ -z $hostname ] ; then echo "Yo, you need to set a hostname"; exit 1; fi files="exclude key" for _file in $files ; do if ! [ -f $_dir/$hostname/$_file ] ; then echo "Can't find file: $_dir/$hostname/$_file"; exit 1; fi done directories="content" for _directory in $directories ; do if ! [ -d $_dir/$hostname/$_directory ] ; then echo "Can't find dir: $_dir/$hostname/$_directory"; exit 1; fi done if [ -e $_dir/$hostname/cfg ] ; then . $_dir/$hostname/cfg; fi if ! [ -d $logdir ] ; then mkdir $logdir ; fi log_file="$logdir/${hostname}.$(date $log_fmt).log" if ! [ -z $_pth ] ; then log_file="$logdir/${hostname}.$_pth.$(date $log_fmt).log" ; fi backup() { rsync -avzHP$rsync_opts --numeric-ids --delete --exclude-from="$_dir/$hostname/$_exc" \ -e "ssh -i $_dir/$hostname/key $ssh_opts" "root@$hostname:/$_pth" "$_dir/$hostname/content/$_pth"; } if [ $log -eq 2 ] ; then backup | tee $log_file; elif [ $log -eq 1 ] ; then backup >> $log_file; else backup; fi