#!/bin/bash rsyncOptions='-vxcaHAXF' rsync --help | \ grep -q -- --may-modify-others && \ rsyncOptions="${rsyncOptions} --may-modify-others" [ -r "#ETCDIR#/backup.conf" ] && \ . "#ETCDIR#/backup.conf" usage() { >&2 echo \ 'Usage: backup /tmp/pidFile /path/to/destination/ user@source:path [proxy_user@ssh_host] Backup files from remote with rsync, possibly via SSH-tunnel. With no arguments, information is expected to be in array $backups in #ETCDIR#/backup.conf with name of executable (e.g. a symlink) as key. Options: /tmp/pidFile location of file to store PID in /path/to/destination location to store backups in user@source:path remote data to back up proxy_user@ssh_host ssh login to proxy node (optional) #HELPTEXT# #' [ -z "$1" ] && exit 1 exit $1 } if [ $# -eq 1 ] then if [ "$1" == "--help" ] then usage 0 elif [ "$1" == "--version" ] then echo '#VERSION#' exit fi fi if [ $# -eq 0 ] then backupID="$(basename $0)" [ -z "${backups[${backupID}]}" ] && usage set /tmp/${backupID}.pid ${backups[${backupID}]} fi Basis="$2" pidFile="$1" QuellIP=$(echo "$3" | sed "s|^[a-zA-Z]*://||; s|^[a-zA-Z]*@||; s|:\?/.*$||") if [ "$#" -eq 3 ] then Quelle="$3" elif [ "$#" -eq 4 ] then sshHopp="$4" lokPort=$[$RANDOM/2+8000] rsyncShell="-e ssh -p${lokPort} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" tunnelBefehl="ssh -t -t -L${lokPort}:${QuellIP}:22 ${sshHopp}" HoppIP="${sshHopp#*@}" Quelle="$(echo "$3" | sed "s|${QuellIP}|127.0.0.1|")" else usage fi if [ -n "${QuellIP}" ] then ping -c1 ${QuellIP} > /dev/null || exit 11 fi if [ -n "${HoppIP}" ] then ping -c1 ${HoppIP} > /dev/null || exit 11 fi if [ ! -d ${Basis} ] then [ $(mount | grep -c "\S\+\s\+\S\+\s\+/var/ftp\s") -eq 0 ] && exit 11 exit 2 fi neues_Datum=${Basis}/$(date "+%Y_%m_%d") neues=${Basis}/aktuell linkdests="" for s in $(ls -1 ${Basis} | sort -r | grep -vm 20 aktuell ) do linkdests="${linkdests} --link-dest ${Basis}/${s}" done if [ ! "$(whoami)" == "root" ] then echo "I need to be root." exit 3 fi [ -w ${Basis} ] || exit 11 [ -e ${neues_Datum} ] && exit 4 [ -e ${pidFile} ] && exit 5 echo $$ > ${pidFile} if [ -n "${tunnelBefehl}" ] then ${tunnelBefehl} & backgroundPid=$! sleep 4 fi for toExclude in "${excludes[@]}" do excludeArgs="${excludeArgs} --exclude ${toExclude}" done if [ ! -e ${neues}/wip ] then mkdir -p ${neues}/wip fi chmod 750 ${neues}{,/wip} chown root:root ${neues}{,/wip} if [ -z "${rsyncShell}" ] then rsync ${rsyncOptions} \ ${linkdests} \ ${excludeArgs} \ ${Quelle} ${neues}/wip/ sleep 1 rsync ${Quelle} else rsync "${rsyncShell}" \ ${rsyncOptions} \ ${linkdests} \ ${excludeArgs} \ ${Quelle} ${neues}/wip/ sleep 1 rsync "${rsyncShell}" ${Quelle} fi erg=$? [ -n "${backgroundPid}" ] && kill ${backgroundPid} if [ ${erg} -eq 0 ] || [ ${erg} -eq 24 ] then chmod o-rwx ${neues}/wip mv ${neues}/wip ${neues_Datum} rmdir ${neues} rm ${pidFile} else rm ${pidFile} exit 11 fi