#!/usr/bin/env bash ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ## - ## - assuming your remote user is called "back", you have ## - to give sudoer rights to him as follow: ## - ## - visudo (edits /etc/sudoers file) ## - ## - add line: ## - back ALL=(root)NOPASSWD:/usr/bin/rsync ## - back ALL=(root)NOPASSWD:/usr/bin/find ## - back ALL=(root)NOPASSWD:/usr/bin/realpath ## - ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ## - load functions ## - . $rcopy_functions_file # --------------------------------------------------- # # -------------------- Variable --------------------- # # err_Log=${LOCK_DIR}/dir.err.log > $err_Log #ssh="/usr/bin/ssh -n" _backupSrcDir="$backup_dir" if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then find=`$ssh ${ssh_user}@$srcHost which find` sudo=`$ssh ${ssh_user}@$srcHost which sudo` realpath=`$ssh ${ssh_user}@$srcHost which realpath` fi # # ------------------ Ende Variable ------------------ # # --------------------------------------------------- # if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then if ! $ssh ${ssh_user}@${srcHost} "$sudo $find '$_backupSrcDir' -maxdepth 0" > /dev/null 2>&1 ; then echolog "\t[ERROR]: $_backupSrcDir not found!\n" exit 0 fi if [ -n "$realpath" ] ; then real_backupSrcDir=`$ssh ${ssh_user}@$srcHost "$sudo $realpath '$_backupSrcDir'"` else real_backupSrcDir="$_backupSrcDir" fi backupSrcParentDir=`$dirname "$real_backupSrcDir"` real_dir_name=`$basename "$real_backupSrcDir"` backupSrcDir="${ssh_user}@${srcHost}:\"$real_backupSrcDir\"" else #if [ ! -e $_backupSrcDir ] ; then if ! $sudo $find "$_backupSrcDir" -maxdepth 0 > /dev/null 2>&1 ; then echolog "\t[ERROR]: $_backupSrcDir not found!\n" exit 0 fi realpath=`which realpath` if [ -n "$realpath" ] ; then real_backupSrcDir=`$realpath "$_backupSrcDir"` else real_backupSrcDir="$_backupSrcDir" fi backupSrcParentDir=`$dirname "$real_backupSrcDir"` real_dir_name="`$basename \"$real_backupSrcDir\"`" backupSrcDir=$real_backupSrcDir fi #if [ $destHost != "localhost" ];then # backup_mirror_dir="${ssh_user}@${destHost}:$backup_mirror_dir" # `$ssh $mkdir -p $backup_mirror_dir/$backupSrcParentDir` #else # $mkdir -p $backup_mirror_dir/$backupSrcParentDir #fi $mkdir -p $backup_mirror_dir/$backupSrcParentDir filedate=`$date +"%Y-%m-%d-%H%M"` if $MIRROR ;then if [ "$_backupSrcDir" != "$real_backupSrcDir" ]; then echolog "\t[ Notice ]: $_backupSrcDir --> $real_backupSrcDir" fi ## - some checks, to avoid backup directories twice ## - if [ -f $dirs_backup_done ];then for _dir in `cat "$dirs_backup_done"` ; do if [ "$real_backupSrcDir" = "$_dir" ];then echolog "\t[ Notice ]: $real_backupSrcDir already backuped\n" exit 0 elif echo $real_backupSrcDir | $grep `echo $_dir/` > /dev/null 2>&1 ; then echolog "\t[ Notice ]: $real_backupSrcDir already backuped\n" exit 0 fi done fi for _dir in $backup_dirs ; do [ -n "$orig_backup_dir" -a "$orig_backup_dir" = $_dir ] && continue if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then if ! $ssh ${ssh_user}@${srcHost} "[ -e $_dir ]" ; then continue fi if [ -n "$realpath" ] ; then _dir_realpath=`$ssh ${ssh_user}@$srcHost $realpath $_dir` else _dir_realpath=$_dir fi else if [ ! -e $_dir ] ; then continue fi if [ -n "$realpath" ] ; then _dir_realpath=`$realpath $_dir` else _dir_realpath=$_dir fi fi ## - check, if the directory to save is a subdirectory of an already ## - backuped directory ## - if echo $real_backupSrcDir | $grep -e "^`echo $_dir_realpath/`" > /dev/null 2>&1 ; then msg="" if [ "$_dir_realpath" != "$_dir" ];then msg="(--> $_dir_realpath)" fi echolog "\t[ Notice ]: $real_backupSrcDir will be backuped within $_dir $msg\n" exit 0 fi done ## - ## - End: some checks, to avoid backup directories twice if [ "$_TEST" = "1" ];then progArgs="-n $progArgs --stats" elif [ "$_DEBUG" -gt 0 ];then progArgs="$progArgs --stats" if [ "$_DEBUG" -gt 1 ];then formats="%t -- %o %f %b Bytes[%l]" fi fi ## - begin timestamp ## - b_timestamp=`$date +"%s"` if [ $_DEBUG -gt 1 -a $_TEST -eq 0 ]; then echolog "$rsync --rsync-path='sudo rsync' $progArgs $logArgs="$formats" $backupSrcDir $backup_mirror_dir/$backupSrcParentDir" 2 $( $rsync --rsync-path='sudo -i -u root rsync' $progArgs $logArgs="$formats" \ "$backupSrcDir" $backup_mirror_dir/$backupSrcParentDir >> $logFile 2> $err_Log exit $? ) else $( $rsync --rsync-path='sudo rsync' $progArgs \ "$backupSrcDir" $backup_mirror_dir/$backupSrcParentDir >> $logFile 2> $err_Log exit $? ) fi retval=$? ## - end timestamp ## - e_timestamp=`$date +"%s"` ## - determin duration ## - _time=`expr $e_timestamp - $b_timestamp` t_h=`expr $_time / 60 / 60` t_rest_h=`expr $_time - $t_h \\* 60 \\* 60` t_m=`expr $t_rest_h / 60` t_s=`expr $t_rest_h - $t_m \\* 60` duration="" if [ $t_h -gt 0 ]; then duration="$t_h h : $t_m min : $t_s sec" elif [ $t_m -gt 0 ];then duration="$t_m min : $t_s sec" else duration="$t_s sec" fi ## - look about errors.. ## - if [ "$retval" != "0" ]; then if [ "$retval" = "24" ]; then echolog "\t[ Notice ] \"$real_backupSrcDir\": \n\t`$cat $err_Log`\n" else echolog "\t[ERROR] Cannot mirror directory \"$real_backupSrcDir\" [ $duration ]\n\t`$cat $err_Log`\n" fi else ## - print durations right-aligned ## - [ -z $right_tabstop ] && right_tabstop=60 info_msg="Directory \"`basename $real_backupSrcDir`\" successfully mirrored" _tmp_string="${info_msg}${duration}" _strlen=${#_tmp_string} _count_blank=`expr $right_tabstop - $_strlen` _str_blanks="" while [ $_count_blank -gt 1 ]; do _str_blanks="$_str_blanks " _count_blank=`expr $_count_blank - 1` done echononl "\t$info_msg" echononl "$_str_blanks" echolog "[ $duration ]" fi echo " $real_backupSrcDir" >> $dirs_backup_done else echolog "\tMIRROR is set to $MIRROR. So nothing to do." fi echolog "" exit 0