From 97e034bd9908a2da1eee87d5c07520bc5df48aea Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 23 Nov 2017 04:30:37 +0100 Subject: [PATCH] Add support of multiplexing ssh. --- conf/rcopy.conf.sample | 8 +- hosts/scripts/dir_backup.sh | 45 ++++++--- hosts/scripts/disksettings_backup.sh | 140 +++++++++++++++++++-------- hosts/scripts/main_part.include | 7 +- 4 files changed, 138 insertions(+), 62 deletions(-) diff --git a/conf/rcopy.conf.sample b/conf/rcopy.conf.sample index 18d9c82..66719a1 100644 --- a/conf/rcopy.conf.sample +++ b/conf/rcopy.conf.sample @@ -217,8 +217,7 @@ rm=`which rm` rsync=`which rsync` scp=`which scp` sgdisk=`which sgdisk` -#ssh=`which ssh` -ssh="`which ssh` -n" +ssh=`which ssh` sort=`which sort` tar=`which tar` tune2fs=`which tune2fs` @@ -230,6 +229,11 @@ sort=`which sort` sync=`which sync` touch=`which touch` +ssh_opts='-o ControlMaster=auto -o ControlPath=/root/.ssh/%r@%h:%p -o ControlPersist=60' +if [[ -n "$ssh" ]] ; then + ssh="$ssh -n $ssh_opts" +fi + export basename dirname awk cat cryptsetup cut date dd e2fsck find fdisk grep gzip hdparm logrotate mkdir mount mv parted ps rm rsync scp ssh sort tar tune2fs umount sfdisk sgdisk su sudo sort sync touch # # ------------------ Ende Programme ----------------- # diff --git a/hosts/scripts/dir_backup.sh b/hosts/scripts/dir_backup.sh index eef5bb2..9b6792a 100755 --- a/hosts/scripts/dir_backup.sh +++ b/hosts/scripts/dir_backup.sh @@ -151,21 +151,40 @@ if $MIRROR ;then ## - 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 $? - ) + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + + 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=$? else - $( - $rsync --rsync-path='sudo rsync' $progArgs \ - "$backupSrcDir" $backup_mirror_dir/$backupSrcParentDir >> $logFile 2> $err_Log - exit $? - ) + if [ $_DEBUG -gt 1 -a $_TEST -eq 0 ]; then + echolog "$rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $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' -e "ssh $ssh_opts" $progArgs \ + "$backupSrcDir" $backup_mirror_dir/$backupSrcParentDir >> $logFile 2> $err_Log + exit $? + ) + fi + retval=$? fi - retval=$? ## - end timestamp ## - diff --git a/hosts/scripts/disksettings_backup.sh b/hosts/scripts/disksettings_backup.sh index c261bc2..f2a35d8 100755 --- a/hosts/scripts/disksettings_backup.sh +++ b/hosts/scripts/disksettings_backup.sh @@ -92,19 +92,27 @@ for _disk in $disks ; do else $hdparm -I $disk > ${LOCK_DIR}/info_${_disk}.txt 2> $err_Log fi - if [ "$?" != "0!" ]; then - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/info_${_disk}.txt $target_dir/info_${_disk}.txt >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$?" != "0" ]; then + echolog "\t[ERROR] Cannot determin identification info for disk $disk: \n\t`$cat $err_Log`\n" + else + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/info_${_disk}.txt $target_dir/info_${_disk}.txt >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/info_${_disk}.txt $target_dir/info_${_disk}.txt >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"info_${_disk}.txt\" successfully saved ---" else echolog "\t[ERROR] Cannot save identification info for disk $disk: \n\t`$cat $err_Log`\n" fi - else - echolog "\t[ERROR] Cannot determin identification info for disk $disk: \n\t`$cat $err_Log`\n" fi if [ "X$parted" = "X" ]; then @@ -134,11 +142,19 @@ for _disk in $disks ; do if [ "$?" != "0" ]; then echolog "\t[ERROR] Cannot determin partition tables for $disk (info file): \n\t`$cat $err_Log`\n" else - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/fdisk_${_disk}.txt $target_dir/fdisk_${_disk}.txt >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/fdisk_${_disk}.txt $target_dir/fdisk_${_disk}.txt >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/fdisk_${_disk}.txt $target_dir/fdisk_${_disk}.txt >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"fdisk_${_disk}.txt\" successfully saved ---" else @@ -152,12 +168,20 @@ for _disk in $disks ; do else $dd if=$disk bs=512 count=1 > ${LOCK_DIR}/${_disk}.mbr 2> $err_Log fi - if [ "$?" != "0!" ]; then - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}.mbr $target_dir/${_disk}.mbr >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$?" = "0" ]; then + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}.mbr $target_dir/${_disk}.mbr >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/${_disk}.mbr $target_dir/${_disk}.mbr >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"${_disk}.mbr\" successfully saved ---" else @@ -173,12 +197,20 @@ for _disk in $disks ; do else $sfdisk -d $disk > ${LOCK_DIR}/${_disk}-partitions.sfdisk 2> $err_Log fi - if [ "$?" != "0!" ]; then - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sfdisk $target_dir/${_disk}-partitions.sfdisk >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$?" = "0" ]; then + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sfdisk $target_dir/${_disk}-partitions.sfdisk >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/${_disk}-partitions.sfdisk $target_dir/${_disk}-partitions.sfdisk >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"${_disk}-partitions.sfdisk\" successfully saved ---" else @@ -203,12 +235,20 @@ for _disk in $disks ; do else $dd if=$partition bs=512 count=1 > ${LOCK_DIR}/boot_sec-${_part}.bin 2> $err_Log fi - if [ "$?" != "0!" ]; then - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/boot_sec-${_part}.bin $target_dir/boot_sec-${_part}.bin >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$?" = "0" ]; then + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/boot_sec-${_part}.bin $target_dir/boot_sec-${_part}.bin >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/boot_sec-${_part}.bin $target_dir/boot_sec-${_part}.bin >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"boot_sec-${_part}.bin\" successfully saved ---" else @@ -238,11 +278,19 @@ for _disk in $disks ; do if [ "$?" != "0" ]; then echolog "\t[ERROR] Cannot determin partition tables for $disk (info file): \n\t`$cat $err_Log`\n" else - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/sgdisk_${_disk}.txt $target_dir/sgdisk_${_disk}.txt >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/sgdisk_${_disk}.txt $target_dir/sgdisk_${_disk}.txt >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/sgdisk_${_disk}.txt $target_dir/sgdisk_${_disk}.txt >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"fdisk_${_disk}.txt\" successfully saved ---" else @@ -257,12 +305,20 @@ for _disk in $disks ; do else $sgdisk -b ${LOCK_DIR}/${_disk}-partitions.sgdisk $disk > /dev/null fi - if [ "$?" != "0!" ]; then - $( - $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sgdisk $target_dir/${_disk}-partitions.sgdisk >> $logFile 2> $err_Log - exit $? - ) - retval=$? + if [ "$?" = "0" ]; then + if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then + $( + $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sgdisk $target_dir/${_disk}-partitions.sgdisk >> $logFile 2> $err_Log + exit $? + ) + retval=$? + else + $( + $rsync --rsync-path='sudo rsync' -e "ssh $ssh_opts" $progArgs ${LOCK_DIR}/${_disk}-partitions.sgdisk $target_dir/${_disk}-partitions.sgdisk >> $logFile 2> $err_Log + exit $? + ) + retval=$? + fi if [ "$retval" = "0" ]; then echolog "\t--- \"${_disk}-partitions.sgdisk\" successfully saved ---" else diff --git a/hosts/scripts/main_part.include b/hosts/scripts/main_part.include index dd9bc3f..f11c13a 100644 --- a/hosts/scripts/main_part.include +++ b/hosts/scripts/main_part.include @@ -44,11 +44,8 @@ if ! $ssh_hopping && ! $ssh_tunnel ; then _via_ssh_tunnel=false export _via_ssh_tunnel - if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then - progArgs="$rsync_progArgs -a -p --delete" - else - progArgs="$rsync_progArgs -a -p -e ssh --delete" - fi + progArgs="$rsync_progArgs -a -p --delete" + else ## - create ssh-tunnel localhost -> $hop_host -> $target_host