Initial import - (formaly a SVN project named rcopy)
This commit is contained in:
		
							
								
								
									
										226
									
								
								hosts/scripts/dir_backup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										226
									
								
								hosts/scripts/dir_backup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,226 @@ | ||||
| #!/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 | ||||
							
								
								
									
										282
									
								
								hosts/scripts/disksettings_backup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										282
									
								
								hosts/scripts/disksettings_backup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,282 @@ | ||||
| #!/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/which | ||||
| ## -    back   ALL=(root)NOPASSWD:/sbin/hdparm -I /dev/* | ||||
| ## -    back   ALL=(root)NOPASSWD:/sbin/fdisk | ||||
| ## -    back   ALL=(root)NOPASSWD:/sbin/sfdisk -d /dev/* | ||||
| ## -    back   ALL=(root)NOPASSWD:/bin/dd if=/dev/* | ||||
| ## -    back   ALL=(root)NOPASSWD:/sbin/parted | ||||
| ## - | ||||
| ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||||
|  | ||||
|  | ||||
|  | ||||
| ## - load functions | ||||
| ## - | ||||
| . $rcopy_functions_file | ||||
|  | ||||
| # --------------------------------------------------- # | ||||
| # -------------------- Variable --------------------- # | ||||
| # | ||||
|  | ||||
| err_Log=${LOCK_DIR}/disksettings.err.log | ||||
| > $err_Log | ||||
|  | ||||
| dirname_diskinfo=diskinfo | ||||
| target_dir=$backup_mirror_dir/$dirname_diskinfo | ||||
|  | ||||
|  | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    sudo=`$ssh ${ssh_user}@$srcHost which sudo` | ||||
|  | ||||
|    fdisk=`$ssh ${ssh_user}@$srcHost $sudo which fdisk` | ||||
|    sfdisk=`$ssh ${ssh_user}@$srcHost $sudo which sfdisk` | ||||
|    hdparm=`$ssh ${ssh_user}@$srcHost $sudo which hdparm` | ||||
|    dd=`$ssh ${ssh_user}@$srcHost which dd` | ||||
|    rm=`$ssh ${ssh_user}@$srcHost which rm` | ||||
|  | ||||
|    parted=`$ssh ${ssh_user}@$srcHost sudo which parted` | ||||
|    sgdisk=`$ssh ${ssh_user}@$srcHost sudo which sgdisk` | ||||
|  | ||||
| fi | ||||
|  | ||||
| # | ||||
| # ------------------ Ende Variable ------------------ # | ||||
| # --------------------------------------------------- # | ||||
|  | ||||
|  | ||||
| if [ $destHost != "localhost" ] ;then | ||||
|    `$ssh $mkdir -p $backup_mirror_dir/$dirname_diskinfo` | ||||
|    target_dir="${ssh_user}@${destHost}:$target_dir" | ||||
| else | ||||
|    $mkdir -p $target_dir | ||||
| fi | ||||
|  | ||||
| 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 | ||||
|  | ||||
| filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
| ## - Ermittle Partitions Typ | ||||
| ## - | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    disks=$($ssh ${ssh_user}@$srcHost "sed -ne 's/^.*\([shm]d[a-zA-Z]\+ *$\)/\1/p' /proc/partitions") | ||||
| else | ||||
|    disks=$(sed -ne 's/^.*\([shm]d[a-zA-Z]\+ *$\)/\1/p' /proc/partitions) | ||||
| fi | ||||
|  | ||||
| for _disk in $disks ; do | ||||
|    disk=/dev/$_disk | ||||
|  | ||||
|    echolog "\n\n\t========" | ||||
|    echolog "\t$disk" | ||||
|    echolog "\t========" | ||||
|  | ||||
|    echolog "\n\tBackup identification info for disk $disk.." | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       $ssh ${ssh_user}@$srcHost "$sudo $hdparm -I $disk" > ${LOCK_DIR}/info_${_disk}.txt 2> $err_Log | ||||
|    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 [ "$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 | ||||
|       echolog "\t[ERROR] program \"parted\" is needed to determin partition type. please install \"parted\"." | ||||
|       continue | ||||
|    fi | ||||
|  | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       #pt_type=$($ssh ${ssh_user}@$srcHost "sudo $parted -s $disk print|sed -ne 's@^.*Table: \(.*\)\$@\1@p'") | ||||
|        pt_type=$($ssh ${ssh_user}@$srcHost "sudo $parted -s $disk print | grep -E \"^.*(Table:|tabelle:)\" | cut -d':' -f2 | tr -d  ' '") | ||||
|    else | ||||
|       #pt_type=$($parted -s $disk print|sed -ne 's@^.*Table: \(.*\)$@\1@p') | ||||
|       pt_type=$($parted -s $disk print | grep -E "^.*(Table:|tabelle:)" | cut -d':' -f2 | tr -d  ' ') | ||||
|    fi | ||||
|  | ||||
|    if [ "$pt_type" = "msdos" ]; then | ||||
|  | ||||
|       echolog "\n\n\tPartitiontype ${disk}: $pt_type" | ||||
|       echolog "\t-----------------------\n" | ||||
|  | ||||
|       echolog "\tBackup partition tables for $disk (info file).." | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $ssh ${ssh_user}@$srcHost "$sudo $fdisk -lu $disk" > ${LOCK_DIR}/fdisk_${_disk}.txt 2>$err_Log | ||||
|       else | ||||
|          $fdisk -lu $disk > ${LOCK_DIR}/fdisk_${_disk}.txt 2>$err_Log | ||||
|       fi | ||||
|       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 [ "$retval" = "0" ]; then | ||||
|             echolog "\t--- \"fdisk_${_disk}.txt\"  successfully saved ---" | ||||
|          else | ||||
|             echolog "\t[ERROR] Cannot save partition tables for $disk (info file): \n\t`$cat $err_Log`\n" | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       echolog "\n\tBackup master boot record of $disk.." | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $ssh ${ssh_user}@$srcHost "$sudo $dd if=$disk  bs=512 count=1" > ${LOCK_DIR}/${_disk}.mbr 2> $err_Log | ||||
|       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 [ "$retval" = "0" ]; then | ||||
|             echolog "\t--- \"${_disk}.mbr\"  successfully saved ---" | ||||
|          else | ||||
|             echolog "\t[ERROR] Cannot save master boot record of disk $disk: \n\t`$cat $err_Log`\n" | ||||
|          fi | ||||
|       else | ||||
|          echolog "\t[ERROR] Cannot copy master boot record of $disk: \n\t`$cat $err_Log`\n" | ||||
|       fi | ||||
|  | ||||
|       echolog "\n\tBackup partition table of $disk (sfdisk).." | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $ssh ${ssh_user}@$srcHost "$sudo $sfdisk -d $disk" > ${LOCK_DIR}/${_disk}-partitions.sfdisk 2> $err_Log | ||||
|       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 [ "$retval" = "0" ]; then | ||||
|             echolog "\t--- \"${_disk}-partitions.sfdisk\"  successfully saved ---" | ||||
|          else | ||||
|             echolog "\t[ERROR] Cannot save partition table of disk $disk: \n\t`$cat $err_Log`\n" | ||||
|          fi | ||||
|       else | ||||
|          echolog "\t[ERROR] Cannot create partition table of of $disk using sfdisk: \n\t`$cat $err_Log`\n" | ||||
|       fi | ||||
|  | ||||
|  | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          partitions=`$ssh ${ssh_user}@$srcHost "$sudo $fdisk -l 2>/dev/null" | grep -e"^$disk" | awk '{print$1}'`  >> $logFile 2> $err_Log | ||||
|       else | ||||
|          partitions=`$fdisk -l 2>/dev/null | grep -e"^$disk" | awk '{print$1}'`  >> $logFile 2> $err_Log | ||||
|       fi | ||||
|       for partition in $partitions ; do | ||||
|          _part=`echo $partition | cut -d"/" -f3` | ||||
|  | ||||
|          echolog "\n\tBackup boot sector of $partition .." | ||||
|          if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|             $ssh ${ssh_user}@$srcHost "$sudo $dd if=$partition bs=512 count=1" > ${LOCK_DIR}/boot_sec-${_part}.bin 2> $err_Log | ||||
|          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 [ "$retval" = "0" ]; then | ||||
|                echolog "\t--- \"boot_sec-${_part}.bin\"  successfully saved ---" | ||||
|             else | ||||
|                echolog "\t[ERROR] Cannot save boot record of partition $partition: \n\t`$cat $err_Log`\n" | ||||
|             fi | ||||
|          else | ||||
|             echolog "\t[ERROR] Cannot copy boot record of ipartition $partition: \n\t`$cat $err_Log`\n" | ||||
|          fi | ||||
|       done | ||||
|  | ||||
|    elif [ "$pt_type" = "gpt" ]; then | ||||
|  | ||||
|       echolog "\n\n\tPartitiontype ${disk}: $pt_type" | ||||
|       echolog "\t-----------------------\n" | ||||
|  | ||||
|       if [ "X$sgdisk" = "X" ]; then | ||||
|          echolog "\t[ERROR] program \"sgdisk\" is needed to determin partition settings. please install \"sgdisk\"." | ||||
|          continue | ||||
|       fi | ||||
|  | ||||
|       echolog "\tBackup partition tables for $disk (info file).." | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $ssh ${ssh_user}@$srcHost "$sudo $sgdisk -p $disk" > ${LOCK_DIR}/sgdisk_${_disk}.txt 2>$err_Log | ||||
|       else | ||||
|          $sgdisk -p $disk > ${LOCK_DIR}/sgdisk_${_disk}.txt 2>$err_Log | ||||
|       fi | ||||
|       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 [ "$retval" = "0" ]; then | ||||
|             echolog "\t--- \"fdisk_${_disk}.txt\"  successfully saved ---" | ||||
|          else | ||||
|             echolog "\t[ERROR] Cannot save partition tables for $disk (info file): \n\t`$cat $err_Log`\n" | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       echolog "\n\tBackup partition table of $disk (sgdisk).." | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $ssh ${ssh_user}@$srcHost "$sudo $sgdisk -b /tmp/${_disk}-partitions.sgdisk $disk" > /dev/null | ||||
|          $scp -p ${ssh_user}@$srcHost:/tmp/${_disk}-partitions.sgdisk ${LOCK_DIR}/${_disk}-partitions.sgdisk > /dev/null 2>&1 | ||||
|       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 [ "$retval" = "0" ]; then | ||||
|             echolog "\t--- \"${_disk}-partitions.sgdisk\"  successfully saved ---" | ||||
|          else | ||||
|             echolog "\t[ERROR] Cannot save partition table of disk $disk: \n\t`$cat $err_Log`\n" | ||||
|          fi | ||||
|       else | ||||
|          echolog "\t[ERROR] Cannot create partition table of of $disk using sgdisk: \n\t`$cat $err_Log`\n" | ||||
|       fi | ||||
|  | ||||
|    else | ||||
|       echolog "\t[ERROR] Cannot determin partition type for disk $disk !!" | ||||
|    fi | ||||
|  | ||||
| done | ||||
| echolog "" | ||||
|  | ||||
| exit 0 | ||||
							
								
								
									
										383
									
								
								hosts/scripts/main_part.include
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										383
									
								
								hosts/scripts/main_part.include
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,383 @@ | ||||
|  | ||||
| ## --------------------------- ## | ||||
| ## -                         - ## | ||||
| ## - !! DON'T CHANGE THIS !! - ## | ||||
| ## -                         - ## | ||||
| ## --------------------------- ## | ||||
|  | ||||
| destHost=localhost | ||||
|  | ||||
| ## - where to store backuped data | ||||
| ## - | ||||
| script_backup_dir=$backup_base_dir/$srcHost | ||||
| backup_archiv_dir=$script_backup_dir/archive | ||||
| backup_mirror_dir=$script_backup_dir/mirror | ||||
|  | ||||
| ## - where to find backup scripts | ||||
| ## - | ||||
| script_dir=$hosts_base_dir/scripts | ||||
|  | ||||
| ## - All backuped directories will be written to that file, to | ||||
| ## - avoid  multiple execution.  | ||||
| ## - | ||||
| dirs_backup_done="$script_backup_dir/backup_dirs.lst" | ||||
|  | ||||
| export destHost script_backup_dir backup_archiv_dir backup_mirror_dir script_dir dirs_backup_done | ||||
|  | ||||
| # | ||||
| # ------------------ Ende Variable ------------------ # | ||||
| # --------------------------------------------------- # | ||||
|  | ||||
| ## - Works even if "ssh_hopping" is not set | ||||
| ## - | ||||
| if [ "X$ssh_hopping" = "X" ] ; then  | ||||
|    ssh_hopping=false  | ||||
| fi | ||||
| ## - Works even if "ssh_tunnel" is not set | ||||
| ## - | ||||
| if [ "X$ssh_tunnel" = "X" ] ; then  | ||||
|    ssh_tunnel=false  | ||||
| fi | ||||
|  | ||||
| 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 | ||||
| else | ||||
|  | ||||
|    ## - create ssh-tunnel localhost -> $hop_host -> $target_host | ||||
|    ## - | ||||
|    progArgs="-a -p -e ssh --delete" | ||||
|    target_host=$srcHost | ||||
|    srcHost=localhost | ||||
|  | ||||
|    _via_ssh_tunnel=true | ||||
|    export _via_ssh_tunnel | ||||
|  | ||||
|    ## - establish ssh tunnel | ||||
|    ## - | ||||
|    if  $ssh_hopping ; then | ||||
|  | ||||
|       _ssh_keyfile_arg="" | ||||
|       if [ "X$ssh_keyfile" != "X" ]; then | ||||
|          _ssh_keyfile_arg=" -i $ssh_keyfile" | ||||
|       fi | ||||
|  | ||||
|       ## -    delete existing ssh tunnels | ||||
|       ## - | ||||
|       tunnel_pid_old=`ps x | grep -e "$local_port:[^:]*:$target_port" | grep -v grep | awk '{print$1}'` | ||||
|        | ||||
|       if [ -n "$tunnel_pid_old" ]; then | ||||
|          kill $tunnel_pid_old | ||||
|       fi | ||||
|  | ||||
|       $ssh -fN -p $hop_host_port $_ssh_keyfile_arg \ | ||||
|            -l $ssh_user \ | ||||
|            -L $local_port:$target_host:$target_port \ | ||||
|            $ssh_hop_host 2> /dev/null | ||||
|  | ||||
|       tunnel_pid=`ps x | grep -e "$local_port:$target_host:$target_port" | grep -v grep | awk '{print$1}'` | ||||
|  | ||||
|    else | ||||
|       if [ "X$ssh_tunnel_target_port" = "X" ]; then | ||||
|          ssh_tunnel_target_port=22 | ||||
|       fi | ||||
|       _ssh_keyfile_arg="" | ||||
|       if [ "X$ssh_tunnel_key_file" != "X" ]; then | ||||
|          _ssh_keyfile_arg="-i $ssh_tunnel_key_file" | ||||
|       fi | ||||
|  | ||||
|       ## -    delete existing ssh tunnels | ||||
|       ## - | ||||
|       tunnel_pid_old=`ps x | grep -e "$ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port" | grep -v grep | awk '{print$1}'` | ||||
|        | ||||
|       if [ -n "$tunnel_pid_old" ]; then | ||||
|          kill $tunnel_pid_old | ||||
|       fi | ||||
|  | ||||
|       $ssh -fN -p $ssh_tunnel_target_port $_ssh_keyfile_arg \ | ||||
|            -l $ssh_user \ | ||||
|            -L $ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port \ | ||||
|            $target_host 2> /dev/null | ||||
|  | ||||
|       tunnel_pid=`ps x | grep -e "$ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port" | grep -v grep | awk '{print$1}'` | ||||
|    fi | ||||
|  | ||||
| fi | ||||
|  | ||||
| logArgs="--log-format" | ||||
| formats="" | ||||
|  | ||||
|  | ||||
| backup_dirs="" | ||||
| for dir in $dir_backup ; do | ||||
|    backup_dirs="$backup_dirs $dir" | ||||
| done | ||||
|  | ||||
| found=true | ||||
| if [ "X$backup_dirs" = "X" ]; then | ||||
|    found=false | ||||
| fi | ||||
|  | ||||
|  | ||||
| if [ "$found" = "true" -o "$pgsql_backup" = "true"  -o "$mysql_backup" = "true" -o "$disksetting_backup" = "true" ] ; then | ||||
|  | ||||
|    begin_h=`date +%H` | ||||
|    begin_m=`date +%M` | ||||
|    backup_date=`date +"%d.%m.%Y"` | ||||
|    backup_begin_timestamp=`date +"%s"` | ||||
|     | ||||
|    if $_via_ssh_tunnel ; then | ||||
|       echolog "\nBegin saving host \"$target_host\" : $backup_date ( ${begin_h}:${begin_m} h )\n" | ||||
|    else | ||||
|       echolog "\nBegin saving host \"$srcHost\" : $backup_date ( ${begin_h}:${begin_m} h )\n" | ||||
|    fi | ||||
|  | ||||
|    if ! $found; then | ||||
|       echolog "[ Notice ]: No directories for backup given..\n" | ||||
|    fi | ||||
|  | ||||
|    ## - BACKUP .. | ||||
|    ## - | ||||
|  | ||||
|    ## - we want run the scripts in a subshell, so we export needed environment | ||||
|    ## - | ||||
|    export srcHost progArgs logArgs formats backup_dirs | ||||
|  | ||||
|  | ||||
|    if  $ARCHIVE ;then | ||||
|  | ||||
|       echolog "\nGoing to handle archives.. ( `$date +%H`:`$date +%M` h )" | ||||
|       _done=false | ||||
|  | ||||
|       if [ ! -d $backup_archiv_dir ]; then | ||||
|         mkdir -p $backup_archiv_dir; | ||||
|       fi | ||||
|  | ||||
|       ## - remove backups older then $days | ||||
|       ## - | ||||
|       archive_dirs=`find $backup_archiv_dir -maxdepth 1 -mindepth 1 -type d -name "Backup_*"` | ||||
|       _today=`date --date "now" +"%Y-%m-%d"` | ||||
|       _timestamp_today=`date --date "$_today" "+%s"` | ||||
|       for _dir in $archive_dirs ; do | ||||
|          _bakup_day=`cat $_dir/BACKUP-DATE` | ||||
|          _backup_timestamp=`date --date "$_bakup_day" +"%s"` | ||||
|          _days=$(echo "scale=0;($_timestamp_today-$_backup_timestamp)/86400" | bc) | ||||
|          if [ $_days -gt $days ]; then | ||||
|             echolog "\n\tRemoving backup from $_bakup_day.." | ||||
|  | ||||
|             ## - begin timestamp | ||||
|             ## - | ||||
|             b_timestamp=`$date +"%s"` | ||||
|  | ||||
|             rm -rf $_dir | ||||
|             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 | ||||
|  | ||||
|             if [ ! "$retval" = 0 ]; then | ||||
|                echo -e "\t[ Error ]: Removing backup from $_bakup_day failed" | ||||
|             else | ||||
|                echolog "\t--- Backup from $_bakup_day successfully removed..    [ $duration ]" | ||||
|             fi | ||||
|          fi | ||||
|          _done=true | ||||
|       done | ||||
|  | ||||
|       ## - Archive last backup.. | ||||
|       ## - | ||||
|       if [ -f $backup_mirror_dir/BACKUP-DATE ]; then | ||||
|          _last_backup_date=`head -n1 $backup_mirror_dir/BACKUP-DATE` | ||||
|          if [ ! -d $backup_archiv_dir/Backup_$_last_backup_date ] ; then | ||||
|             echolog "\n\tArchiving (last) backup from $_last_backup_date..  ( `$date +%H`:`$date +%M` h )" | ||||
|  | ||||
|             ## - begin timestamp | ||||
|             ## - | ||||
|             b_timestamp=`$date +"%s"` | ||||
|  | ||||
|             cp  -ral $backup_mirror_dir $backup_archiv_dir/Backup_$_last_backup_date | ||||
|  | ||||
|             ## - 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 | ||||
|  | ||||
|             if [ ! "$?" = 0 ]; then | ||||
|                echo -e "\t[ Error ]: Archiving backup from $_last_backup_date failed" | ||||
|             else | ||||
|                echolog "\t--- Backup from $_last_backup_date successfully archived..   [ $duration ]" | ||||
|             fi | ||||
|          fi | ||||
|  | ||||
|          _done=true | ||||
|  | ||||
|       fi | ||||
|  | ||||
|       if ! $_done ; then | ||||
|          echolog "\n\tNothing to do. - No last backup found, no backup older then $days days found." | ||||
|       fi | ||||
|       echolog "" | ||||
|  | ||||
|    fi | ||||
|  | ||||
|    if $disksetting_backup ; then | ||||
|       echolog "\nGoing to backup disk settings..  ( `$date +%H`:`$date +%M` h )" | ||||
|       $script_dir/disksettings_backup.sh | ||||
|    fi | ||||
|    if $svn_backup;then | ||||
|       echolog "\nGoing to backup svn repositories..  ( `$date +%H`:`$date +%M` h )" | ||||
|       export svn_source_base_path svn_gzip | ||||
|       $script_dir/svn_backup.sh | ||||
|    fi | ||||
|    if $mysql_backup ;then | ||||
|       echolog "\nGoing to backup mysql databases..  ( `$date +%H`:`$date +%M` h )" | ||||
|       export mysql_user mysql_password mysql_credential_args mysql_gzip | ||||
|       $script_dir/mysql_backup.sh | ||||
|       ## - !!  | ||||
|       if $restart_apache ;then | ||||
|          $ssh ${ssh_user}@$srcHost "sudo /etc/init.d/apache2 restart" | ||||
|       fi | ||||
|    fi | ||||
|    if $pgsql_backup;then | ||||
|       echolog "\nGoing to backup postgres databases..  ( `$date +%H`:`$date +%M` h )" | ||||
|       export pgsql_user pgsql_gzip | ||||
|       $script_dir/pgsql_backup.sh | ||||
|    fi | ||||
|  | ||||
|    echolog "\nGoing to backup directories..  ( `$date +%H`:`$date +%M` h )" | ||||
|    _done=false | ||||
|  | ||||
|    for dir in $dir_backup ; do | ||||
|       echolog "\n\tDirectory $dir" | ||||
|       backup_dir=$dir $script_dir/dir_backup.sh | ||||
|       _done=true | ||||
|    done | ||||
|  | ||||
|    if ! $_done ; then | ||||
|       echo -e "\t[ Warning ]: No directory for backup selected !!" | ||||
|    fi | ||||
|  | ||||
|  | ||||
|    if $mount_netdir; then | ||||
|       echolog "\nGoing to backup network directories..  ( `$date +%H`:`$date +%M` h )" | ||||
|       for dir in $net_mounted_dir_backup ; do | ||||
|          echolog "\n\tBackup network directory $dir" | ||||
|          export mount_netdir  | ||||
|          backup_dir=$dir $script_dir/net_mounted_dir_backup.sh | ||||
|       done | ||||
|    fi | ||||
|  | ||||
|    end_h=`$date +%H` | ||||
|    end_m=`$date +%M` | ||||
|    backup_date=`$date +"%d.%m.%Y"` | ||||
|    backup_end_timestamp=`date +"%s"` | ||||
|  | ||||
|    backup_time=`expr $backup_end_timestamp - $backup_begin_timestamp` | ||||
|    backup_h=`expr $backup_time / 60 / 60` | ||||
|    backup_rest_h=`expr $backup_time - $backup_h \\* 60 \\* 60` | ||||
|    backup_m=`expr $backup_rest_h / 60` | ||||
|    backup_s=`expr $backup_rest_h - $backup_m \\* 60` | ||||
|  | ||||
|    if [ $backup_m -lt 10 ] ;then | ||||
|       backup_m="0$backup_m" | ||||
|    fi | ||||
|    if [ $backup_s -lt 10 ] ;then | ||||
|       backup_s="0$backup_s" | ||||
|    fi | ||||
|  | ||||
|    rm -f $dirs_backup_done | ||||
|  | ||||
|    if $_via_ssh_tunnel ; then | ||||
|       echolog "\nEnd saving host \"$target_host\" : $backup_date ( ${end_h}:${end_m} h ) - duration: ${backup_h} h :${backup_m} min : ${backup_s} sec\n\n" | ||||
|    else | ||||
|       echolog "\nEnd saving host \"$srcHost\" : $backup_date ( ${end_h}:${end_m} h ) - duration: ${backup_h} h :${backup_m} min : ${backup_s} sec\n\n" | ||||
|    fi | ||||
|  | ||||
|    ## - write duration time (a little bit formatted) to logfile of durations | ||||
|    ## - | ||||
|  | ||||
|    ## - determin duration | ||||
|    ## - | ||||
|    duration="" | ||||
|    if [ $backup_h -gt 0 ]; then | ||||
|       duration="$backup_h h : $backup_m min : $t_s sec" | ||||
|    elif [ $backup_m -gt 0 ];then | ||||
|       duration="$backup_m min : $backup_s sec" | ||||
|    else | ||||
|       duration="$backup_s sec" | ||||
|    fi | ||||
|  | ||||
|    if $_via_ssh_tunnel ; then | ||||
|       _host=$target_host | ||||
|    else | ||||
|       _host=$srcHost | ||||
|    fi | ||||
|    _tmp_string="${_host}${duration}" | ||||
|    _strlen=${#_tmp_string} | ||||
|    _count_blank=`expr $right_tabstop - $_strlen` | ||||
|    echo -n -e "   $_host:" >> $logDuration | ||||
|    _str_blanks="" | ||||
|    while [ $_count_blank -gt 1 ]; do | ||||
|       _str_blanks="$_str_blanks " | ||||
|       _count_blank=`expr $_count_blank - 1` | ||||
|    done | ||||
|    echo -n "$_str_blanks" >> $logDuration | ||||
|    echo -n " " >> $logDuration | ||||
|    echo -n -e "$duration\n" >> $logDuration | ||||
|  | ||||
|    ## - Set backup date | ||||
|    ## - | ||||
|    ## - Notice!! | ||||
|    ## -    We ave to remove olf file and set a new one, because we are | ||||
|    ## -    working with hard links | ||||
|    ## - | ||||
|    [ -f $backup_mirror_dir/BACKUP-DATE ] && rm -f $backup_mirror_dir/BACKUP-DATE | ||||
|    [ -d $backup_mirror_dir ] && \ | ||||
|       echo `date --date "now" +"%Y-%m-%d"` > $backup_mirror_dir/BACKUP-DATE | ||||
|  | ||||
| else | ||||
|    echolog "\n[WARNING HOST \"$srcHost\"]: Nothing was marked for backup !!\n" | ||||
| fi | ||||
|  | ||||
| if $_via_ssh_tunnel ; then | ||||
|    kill $tunnel_pid | ||||
| fi | ||||
							
								
								
									
										625
									
								
								hosts/scripts/mysql_backup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										625
									
								
								hosts/scripts/mysql_backup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,625 @@ | ||||
| #!/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 | ||||
| ## - | ||||
| ## - on the remote database allow the congigured mysql user to | ||||
| ## - access the database from localhost an give him the follwing | ||||
| ## - privileges: | ||||
| ## -    Select_priv | ||||
| ## -    Lock_tables_priv | ||||
| ## -    Show_view_priv | ||||
| ## -    Event_priv | ||||
| ## -    Process_priv (since MySQL 5.5) | ||||
| ## -  | ||||
| ## - INSERT INTO user (Host,User,Password,Select_priv,Lock_tables_priv,Show_view_priv,Event_priv) VALUES('localhost','backup',password('backup'),'Y','Y','Y','Y'); | ||||
| ## - | ||||
| ## - Since MySQL 5.5 - you also have to add process privileges (Process_priv = 'Y') | ||||
| ## - INSERT INTO user (Host,User,Password,Select_priv,Process_priv,Lock_tables_priv,Show_view_priv,Event_priv) VALUES('localhost','backup',password('backup'),'Y','Y','Y','Y','Y'); | ||||
| ## - | ||||
| ## - | ||||
| ## - Since MySQL 5.7.x - you also have to add process privileges (Execute_priv = 'Y') | ||||
| ## -                     Password field is now: "authentication_string" | ||||
| ## - INSERT INTO user (Host,User,authentication_string,Select_priv,Process_priv,Lock_tables_priv,Show_view_priv,Event_priv,Execute_priv,ssl_cipher,x509_issuer,x509_subject) VALUES('localhost','backup',password('backup'),'Y','Y','Y','Y','Y','Y','','',''); | ||||
| ## - | ||||
| ## - or if updating from older mysql version: | ||||
| ## - | ||||
| ## - UPDATE user SET Execute_priv = 'Y' WHERE User = 'backup'; | ||||
| ## -  | ||||
| ## - | ||||
| ## - FLUSH PRIVILEGES; | ||||
| ## -  | ||||
| ## -  | ||||
| ## - Since Version 5.6, giving password on command line is considered as insecure.  | ||||
| ## - To avoid giving the password on command line, you can use an  | ||||
| ## - encrypted option file instead. | ||||
| ## - | ||||
| ## -    1.) Create (encrypted) option file: | ||||
| ## - | ||||
| ## -    Debian package installation: | ||||
| ## -    $ mysql_config_editor set --login-path=local --socket=/var/run/mysqld/mysqld.sock  --user=backup --password | ||||
| ## -    $ Password: | ||||
| ## - | ||||
| ## -    Source installation: | ||||
| ## -    $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock  --user=backup --password | ||||
| ## -    $ Password: | ||||
| ## - | ||||
| ## -    2.) Set environment variable mysql_credential_args="--login-path=local" | ||||
| ## -    Now, the backup script uses the encrypted option file instead of (unencrypt) password | ||||
| ## -    on command line. | ||||
| ## - | ||||
| ## - | ||||
| ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||||
|  | ||||
| err_Log=${LOCK_DIR}/mysql.err.log | ||||
| > $err_Log | ||||
|  | ||||
|  | ||||
| ## - load functions | ||||
| ## - | ||||
| . $rcopy_functions_file | ||||
|  | ||||
| # --------------------------------------------------- # | ||||
| # -------------------- Variable --------------------- # | ||||
| # | ||||
|  | ||||
| _backupDestArchiveDir="${script_backup_dir}/MySQL" | ||||
|  | ||||
|  | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    mysql=`$ssh ${ssh_user}@$srcHost which mysql` | ||||
|    mysqldump=`$ssh ${ssh_user}@$srcHost which mysqldump` | ||||
|    ssh_options="-o BatchMode=yes -o ConnectTimeout=360" | ||||
| else | ||||
|    mysql=`which mysql` | ||||
|    mysqldump=`which mysqldump` | ||||
| fi | ||||
|  | ||||
| if [ -z "$mysql" ]; then | ||||
|    mysql="/usr/local/mysql/bin/mysql" | ||||
| fi | ||||
|  | ||||
| if [ -z "$mysqldump" ]; then | ||||
|    mysqldump="/usr/local/mysql/bin/mysqldump" | ||||
| fi | ||||
|  | ||||
| # | ||||
| # ------------------ Ende Variable ------------------ # | ||||
| # --------------------------------------------------- # | ||||
|  | ||||
|     | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    if [ -n "$mysql_credential_args" ] ; then | ||||
|       DATABASES=`$ssh ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -e \"show databases\""` | ||||
|       MYSQL_VERSION=`$ssh ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -e \"SELECT VERSION()\""` | ||||
|    else | ||||
|       DATABASES=`$ssh ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -e \"show databases\""` | ||||
|       MYSQL_VERSION=`$ssh ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -e \"SELECT VERSION()\""` | ||||
|    fi | ||||
| else | ||||
|    if [ -n "$mysql_credential_args" ] ; then | ||||
|       DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"` | ||||
|       MYSQL_VERSION=`$mysql $mysql_credential_args -N -s -e "SELECT VERSION()"` | ||||
|    else | ||||
|       DATABASES=`$mysql -u$mysql_user -p$mysql_password -N -s -e "show databases"` | ||||
|       MYSQL_VERSION=`$mysql -u$mysql_user -p$mysql_password -N -s -e "SELECT VERSION()"` | ||||
|    fi | ||||
| fi | ||||
|  | ||||
| MYSQL_VERSION_NUM=`echo $MYSQL_VERSION | awk -F \. '{ printf "%02d", $1; printf "%02d", $2; printf "%02d", $3 }'` | ||||
|  | ||||
| if [ ! -d $_backupDestArchiveDir ]; then | ||||
|    mkdir -p $_backupDestArchiveDir | ||||
| fi | ||||
|  | ||||
| echolog "" | ||||
| if $ARCHIVE ;then | ||||
|  | ||||
|    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 | ||||
|  | ||||
|    ## ----------------------------- | ||||
|    ## - Backup SQL Grants for users | ||||
|    ## -  | ||||
|    info_msg="save SQL Grants for users" | ||||
|    echononl "\t$info_msg" | ||||
|  | ||||
|    ## - begin timestamp | ||||
|    ## - | ||||
|    b_timestamp=`$date +"%s"` | ||||
|  | ||||
|    filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       if [ -n "$mysql_credential_args" ] ; then | ||||
|          $( | ||||
|             $ssh $ssh_options ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -A -e\"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''\" | $mysql $mysql_credential_args -N -s -A | sed 's/\$/;/g'" \ | ||||
|                > ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       else | ||||
|          $( | ||||
|             $ssh $ssh_options ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -A -e\"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''\" | $mysql -u$mysql_user -p$mysql_password -N -s -A | sed 's/\$/;/g'" \ | ||||
|                > ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       fi | ||||
|    else | ||||
|       if [ -n "$mysql_credential_args" ] ; then | ||||
|          $( | ||||
|             $mysql $mysql_credential_args -N -s -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | $mysql $mysql_credential_args -N -s -A | sed 's/\$/;/g' \ | ||||
|                > ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       else | ||||
|          $( | ||||
|             $mysql -u$mysql_user -p$mysql_password -N -s -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | $mysql -u$mysql_user -p$mysql_password -N -s -A | sed 's/\$/;/g' \ | ||||
|                > ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       fi | ||||
|    fi | ||||
|        | ||||
|    err_msg="Cannot save SQL Grants for users" | ||||
|    keep_backup_on_error=false | ||||
|  | ||||
|    if [ "$retval" = 0 ];then | ||||
|       [ -z $mysql_gzip ] && mysql_gzip=false | ||||
|       if $mysql_gzip ; then | ||||
|          $gzip ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log | ||||
|          retval=$? | ||||
|          err_msg="Cannot gzip \"MySQLGrants.sql-${filedate}.sql\"" | ||||
|          keep_backup_on_error=true | ||||
|       fi | ||||
|    fi | ||||
|  | ||||
|    ## - 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 | ||||
|       echolog "" | ||||
|       echolog "\t[ERROR] ${err_msg}  [  $duration ]\n\t`$cat $err_Log`\n" | ||||
|       if ! $keep_backup_on_error ; then | ||||
|          rm -f ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql | ||||
|       fi | ||||
|    else | ||||
|  | ||||
|       ## - print durations right-aligned | ||||
|       ## - | ||||
|       [ -z $right_tabstop ] && right_tabstop=65 | ||||
|       _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 "$_str_blanks" | ||||
|  | ||||
|       echolog "  [ $duration ]" | ||||
|    fi | ||||
|  | ||||
|    ## - End: Backup SQL Grants for users | ||||
|    ## ---------------------------------- | ||||
|  | ||||
|    ## ----------------------------- | ||||
|    ## - Backup/Create Database Craetion sql statements  | ||||
|    ## -  | ||||
|    info_msg="create database creation SQL file" | ||||
|    echononl "\t$info_msg" | ||||
|     | ||||
|    filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|    > ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql | ||||
|  | ||||
|    ## - begin timestamp | ||||
|    ## - | ||||
|    b_timestamp=`$date +"%s"` | ||||
|  | ||||
|    retval=0 | ||||
|    for i in $DATABASES ; do | ||||
|  | ||||
|       if [ "$i" = "information_schema" -o "$i" = "performance_schema" ];then | ||||
|          continue | ||||
|       fi | ||||
|  | ||||
|       echo "CREATE DATABASE IF NOT EXISTS $i CHARACTER SET utf8 COLLATE utf8_general_ci;" \ | ||||
|          >> ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql | ||||
|  | ||||
|       if [ "$?" != 0 ]; then | ||||
|          retval=1 | ||||
|       fi | ||||
|    done       | ||||
|  | ||||
|    err_msg="Cannot create Database Creation SQL file" | ||||
|    keep_backup_on_error=false | ||||
|  | ||||
|    if [ "$retval" = 0 ];then | ||||
|       [ -z $mysql_gzip ] && mysql_gzip=false | ||||
|       if $mysql_gzip ; then | ||||
|          $gzip ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql 2> $err_Log | ||||
|          retval=$? | ||||
|          err_msg="Cannot gzip \"MySQLGrants.sql-${filedate}.sql\"" | ||||
|          keep_backup_on_error=true | ||||
|       fi | ||||
|    fi | ||||
|  | ||||
|    ## - 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 | ||||
|       echolog "" | ||||
|       echolog "\t[ERROR] ${err_msg}  [  $duration ]\n\t`$cat $err_Log`\n" | ||||
|       if ! $keep_backup_on_error ; then | ||||
|          rm -f ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql | ||||
|       fi | ||||
|    else | ||||
|  | ||||
|       ## - print durations right-aligned | ||||
|       ## - | ||||
|       [ -z $right_tabstop ] && right_tabstop=65 | ||||
|       _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 "$_str_blanks" | ||||
|  | ||||
|       echolog "  [ $duration ]" | ||||
|    fi | ||||
|  | ||||
|    ## - End: Create Database Craetion sql statements  | ||||
|    ## ----------------------------------------------------- | ||||
|  | ||||
|  | ||||
|    ## - backup all databases except the those from midgard.. | ||||
|    ## - | ||||
|    for i in $DATABASES ; do | ||||
|  | ||||
|       ## ------------------------------------ | ||||
|       ## - Save Functions seperatly if exists | ||||
|       ## - | ||||
|       declare -i COUNT_FUNCTION=0 | ||||
|  | ||||
|     | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          if [ -n "$mysql_credential_args" ] ; then | ||||
|             COUNT_FUNCTION=`$ssh ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -e \"SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'\""` | ||||
|          else | ||||
|             COUNT_FUNCTION=`$ssh ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -e \"SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'\""` | ||||
|          fi | ||||
|       else | ||||
|          if [ -n "$mysql_credential_args" ] ; then | ||||
|             COUNT_FUNCTION=`$mysql $mysql_credential_args -N -s -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'"` | ||||
|          else | ||||
|             COUNT_FUNCTION=`$mysql -u$mysql_user -p$mysql_password -N -s -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'"` | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       if [ $COUNT_FUNCTION -gt 0 ] ; then | ||||
|  | ||||
|          ## - begin timestamp | ||||
|          ## - | ||||
|          b_timestamp=`$date +"%s"` | ||||
|  | ||||
|          filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|          mysqldump_flags="--no-data --no-create-info --routines" | ||||
|  | ||||
|          info_msg="save functions of database $i.." | ||||
|          echononl "\t$info_msg" | ||||
|          if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|             if [ -n "$mysql_credential_args" ] ; then | ||||
|                $( | ||||
|                   $ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump $mysql_credential_args $mysqldump_flags $i" \ | ||||
|                      > ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log | ||||
|                   exit $? | ||||
|                ) | ||||
|                retval=$? | ||||
|             else | ||||
|                $( | ||||
|                   $ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i" \ | ||||
|                      > ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log | ||||
|                   exit $? | ||||
|                ) | ||||
|                retval=$? | ||||
|             fi | ||||
|          else | ||||
|             if [ -n "$mysql_credential_args" ] ; then | ||||
|                $( | ||||
|                   $mysqldump $mysql_credential_args $mysqldump_flags $i \ | ||||
|                      > ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log | ||||
|                   exit $? | ||||
|                ) | ||||
|                retval=$? | ||||
|             else | ||||
|                $( | ||||
|                   $mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i \ | ||||
|                      > ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql  2> $err_Log | ||||
|                   exit $? | ||||
|                ) | ||||
|                retval=$? | ||||
|             fi | ||||
|          fi | ||||
|          err_msg="Cannot save functions of database \"$i\"" | ||||
|          keep_backup_on_error=false | ||||
|  | ||||
|          if [ "$retval" = 0 ];then | ||||
|             [ -z $mysql_gzip ] && mysql_gzip=false | ||||
|             if $mysql_gzip ; then | ||||
|                $gzip ${_backupDestArchiveDir}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log | ||||
|                retval=$? | ||||
|                err_msg="Cannot gzip \"MySQLStoredProcedures.${i}-${filedate}.sql\"" | ||||
|                keep_backup_on_error=true | ||||
|             fi | ||||
|          fi | ||||
|  | ||||
|          ## - 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 | ||||
|             echolog "" | ||||
|             echolog "\t[ERROR] ${err_msg}  [  $duration ]\n\t`$cat $err_Log`\n" | ||||
|             if ! $keep_backup_on_error ; then | ||||
|                rm -f ${_backupDestArchiveDir}/MySQLStoredProcedures.${i}-${filedate}.sql | ||||
|             fi | ||||
|          else | ||||
|  | ||||
|             ## - print durations right-aligned | ||||
|             ## - | ||||
|             [ -z $right_tabstop ] && right_tabstop=65 | ||||
|             _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 "$_str_blanks" | ||||
|  | ||||
|             echolog "  [ $duration ]" | ||||
|          fi | ||||
|  | ||||
|       fi | ||||
|  | ||||
|       ## - End: Save Functions seperatly if exists | ||||
|       ## ----------------------------------------- | ||||
|  | ||||
|  | ||||
|       ## -------------- | ||||
|       ## - Dump Databas | ||||
|  | ||||
|       filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|       ## - begin timestamp | ||||
|       ## - | ||||
|       b_timestamp=`$date +"%s"` | ||||
|  | ||||
|       ## - Like --opt but without --extended-insert. So  | ||||
|       mysqldump_flags="--protocol=SOCKET --max_allowed_packet=128M --skip-opt --add-drop-table --add-locks --create-options --quick --set-charset --disable-keys --lock-tables --routines" | ||||
|  | ||||
|       if [ "$i" = "information_schema" -o "$i" = "performance_schema" ];then | ||||
|          mysqldump_flags="$mysqldump_flags --single-transaction" | ||||
|          if [ "$i" = "information_schema" -a "$MYSQL_VERSION_NUM" -ge 050700 ]; then | ||||
|             ## - As of MySQL 5.7.6, the Performance Schema also contains system and status variable tables. | ||||
|             ## - | ||||
|             ## - The Performance Schema tables are intended to replace the INFORMATION_SCHEMA tables,  | ||||
|             ## - which are deprecated as of MySQL 5.7.6 and will be removed in a future MySQL release. | ||||
|             ## - | ||||
|             ## - As of MySQL 5.7.6, the value of the "show_compatibility_56" system variable  | ||||
|             ## - affects the information available from "information_schema". If tis variable | ||||
|             ## - is set to "OFF" (the default), mysqldump produces the tollowing error: | ||||
|             ## - | ||||
|             ## -    Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `GLOBAL_STATUS`':  | ||||
|             ## -    The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation  | ||||
|             ## -    for 'show_compatibility_56' (3167) | ||||
|             ## - | ||||
|             ## - We will ignore errors (force) and also we don#t want error messages | ||||
|             ## - | ||||
|             mysqldump_flags="$mysqldump_flags --force --log-error=/dev/null" | ||||
|          fi | ||||
|       elif [ "$i" = "mysql" -a "$MYSQL_VERSION_NUM" -ge 050500 ]; then | ||||
|          mysqldump_flags="$mysqldump_flags --events" | ||||
|       fi | ||||
|  | ||||
|       if [ "$destHost" = "localhost" ]; then | ||||
|          if [ ! -d ${_backupDestArchiveDir}/${i} ] ; then  | ||||
|             $mkdir -p ${_backupDestArchiveDir}/${i} | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       info_msg="save database $i.." | ||||
|       echononl "\t$info_msg" | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          if [ -n "$mysql_credential_args" ] ; then | ||||
|             $( | ||||
|                $ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump $mysql_credential_args $mysqldump_flags $i" \ | ||||
|                   > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|                exit $? | ||||
|             ) | ||||
|             retval=$? | ||||
|          else | ||||
|             $( | ||||
|                $ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i" \ | ||||
|                   > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|                exit $? | ||||
|             ) | ||||
|             retval=$? | ||||
|          fi | ||||
|       else | ||||
|          if [ -n "$mysql_credential_args" ] ; then | ||||
|             $( | ||||
|                $mysqldump $mysql_credential_args $mysqldump_flags $i \ | ||||
|                   > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|                exit $? | ||||
|             ) | ||||
|             retval=$? | ||||
|          else | ||||
|             $( | ||||
|                $mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i \ | ||||
|                   > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql  2> $err_Log | ||||
|                exit $? | ||||
|             ) | ||||
|             retval=$? | ||||
|          fi | ||||
|       fi | ||||
|       err_msg="Cannot save database \"$i\"" | ||||
|       keep_backup_on_error=false | ||||
|  | ||||
|       if [ "$i" = "information_schema" -a "$MYSQL_VERSION_NUM" -ge 050700 ]; then | ||||
|          ## - As sayed befor: in that case, we want ignore all errors taken from that dump | ||||
|          ## - | ||||
|          retval=0 | ||||
|       fi | ||||
|  | ||||
|       if [ "$retval" = 0 ];then | ||||
|          [ -z $mysql_gzip ] && mysql_gzip=false | ||||
|          if $mysql_gzip ; then | ||||
|             $gzip ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|             retval=$? | ||||
|             err_msg="Cannot gzip database \"$i\"" | ||||
|             keep_backup_on_error=true | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       ## - 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 | ||||
|          echolog "" | ||||
|          echolog "\t[ERROR] ${err_msg}  [  $duration ]\n\t`$cat $err_Log`\n" | ||||
|          if ! $keep_backup_on_error ; then | ||||
|             rm -f ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql | ||||
|          fi | ||||
|          continue | ||||
|       else | ||||
|  | ||||
|          ## - print durations right-aligned | ||||
|          ## - | ||||
|          [ -z $right_tabstop ] && right_tabstop=65 | ||||
|          _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 "$_str_blanks" | ||||
|  | ||||
|          echolog "  [ $duration ]" | ||||
|       fi | ||||
|  | ||||
|       ## - End: Dump Databas | ||||
|       ## ------------------- | ||||
|  | ||||
|    done | ||||
|  | ||||
|    ## /usr/bin/killall -HUP mysqld | ||||
|  | ||||
| else | ||||
|    echolog "\tARCHIVE is set to $ARCHIVE. So nothing to do."    | ||||
| fi | ||||
| echolog "" | ||||
|  | ||||
|  | ||||
| ## - remove all files older than $days days.. | ||||
| ## -  | ||||
| $find $_backupDestArchiveDir -type f -mtime +${days} -exec rm {} \; | ||||
							
								
								
									
										59
									
								
								hosts/scripts/net_mounted_dir_backup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										59
									
								
								hosts/scripts/net_mounted_dir_backup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| #!/usr/bin/env bash | ||||
|  | ||||
| ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||||
| ## - | ||||
| ## - If network directory have to mount (its only possible if backuping | ||||
| ## - localhost) you need an entry in your /etc/fstab, becaus we mount thuch | ||||
| ## - remote directories with "mount <remote directory) | ||||
| ## - | ||||
| ## - i.e. for a samba mounted directory you need an entry like: | ||||
| ## - | ||||
| ## -    //192.168.100.20/qinter /mnt/file-win7/qinter smbfs noauto,user=Administrator,password=chnarzfoo5 0 0 | ||||
| ## - | ||||
| ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||||
|  | ||||
|  | ||||
| ## - load functions | ||||
| ## - | ||||
| . $rcopy_functions_file | ||||
|     | ||||
| if $mount_netdir ; then | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       echolog "\t[ERROR] Mounting network directories is only possible for localhost - yet" | ||||
|       exit 1 | ||||
|    else | ||||
|       ## - Network directory mounted ? | ||||
|       ## - | ||||
|       if ! df | grep "$backup_dir" > /dev/null 2>&1 ;then | ||||
|          $mount $backup_dir > /dev/null 2>&1 | ||||
|       fi | ||||
|  | ||||
|       sleep 5 | ||||
|  | ||||
|       if ! df | grep "$backup_dir" > /dev/null 2>&1 ;then | ||||
|          echolog "\t[ERROR] Cannot mount network directory to \"$backup_dir\"" | ||||
|          exit 1 | ||||
|       fi | ||||
|    fi | ||||
| fi | ||||
|  | ||||
| ## - Check if directory is empty | ||||
| ## - | ||||
| if [ ! "$(ls -A $backup_dir)" ]; then | ||||
|    echolog "\t[ NOTICE ]: Directory \"$backup_dir\" is empty. So nothing to do..\n" | ||||
|    exit 0 | ||||
| fi     | ||||
|     | ||||
| echolog "\n\tBackup network directory \"$backup_dir\"  ( `$date +%H`:`$date +%M` h )" | ||||
| backup_dir=$backup_dir $script_dir/dir_backup.sh | ||||
|        | ||||
| if $mount_netdir ; then | ||||
|    if [ $srcHost != "localhost" ];then | ||||
|       echolog "\t[ERROR] Mounting network directories is only possible for localhost - yet" | ||||
|       exit 1 | ||||
|    else | ||||
|       $umount $backup_dir 2> /dev/null | ||||
|    fi | ||||
| fi | ||||
|  | ||||
| exit 0 | ||||
							
								
								
									
										376
									
								
								hosts/scripts/pgsql_backup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										376
									
								
								hosts/scripts/pgsql_backup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,376 @@ | ||||
| #!/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 lines: | ||||
| ## -    back ALL = (postgres) NOPASSWD: /usr/bin/psql | ||||
| ## -    back ALL = (postgres) NOPASSWD: /usr/bin/pg_dump | ||||
| ## -    back ALL = (postgres) NOPASSWD: /usr/bin/pg_dumpall | ||||
| ## - | ||||
| ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||||
|  | ||||
| err_Log=${LOCK_DIR}/pgsql.err.log | ||||
| > $err_Log | ||||
|  | ||||
|  | ||||
| ## - load functions | ||||
| ## - | ||||
| . $rcopy_functions_file | ||||
|  | ||||
| # --------------------------------------------------- # | ||||
| # -------------------- Variable --------------------- # | ||||
| # | ||||
|  | ||||
| _backupDestArchiveDir="${script_backup_dir}/PostgreSQL" | ||||
|  | ||||
| if [ ! -d $_backupDestArchiveDir ]; then | ||||
|    mkdir -p $_backupDestArchiveDir | ||||
| fi | ||||
|  | ||||
|  | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    psql=`$ssh ${ssh_user}@$srcHost which psql` | ||||
|    pg_dump=`$ssh ${ssh_user}@$srcHost which pg_dump` | ||||
|    pg_dumpall=`$ssh ${ssh_user}@$srcHost which pg_dumpall` | ||||
|    sudo=`$ssh ${ssh_user}@$srcHost which sudo` | ||||
|    su=`$ssh ${ssh_user}@$srcHost which su` | ||||
|    ssh_options="-o BatchMode=yes -o ConnectTimeout=360" | ||||
| else | ||||
|    psql=`which psql` | ||||
|    pg_dump=`which pg_dump` | ||||
|    pg_dumpall=`which pg_dumpall` | ||||
| fi | ||||
|  | ||||
| if [ -z "$psql" ]; then | ||||
|    psql="/usr/local/pgsql/bin/psql" | ||||
| fi | ||||
|  | ||||
| if [ -z "$pg_dump" ]; then | ||||
|    pg_dump="/usr/local/pgsql/bin/pg_dump" | ||||
| fi | ||||
|  | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    DATABASES=`$ssh ${ssh_user}@$srcHost "$sudo -u $pgsql_user $psql  -lt" \ | ||||
|       | $grep -v -e"^$" | $awk '{print$1}'`  | ||||
| else | ||||
|    DATABASES=`$su - $pgsql_user -c "$psql  -lt" | $grep -v -e"^$" | $awk '{print$1}'` | ||||
| fi | ||||
|  | ||||
|  | ||||
| # ------------------ Ende Variable ------------------ # | ||||
| # --------------------------------------------------- # | ||||
|  | ||||
| echolog "" | ||||
| if $ARCHIVE ;then | ||||
|  | ||||
|    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 | ||||
|  | ||||
|    ## --------------------------- | ||||
|    ## - Backup PostgreSQL's Roles | ||||
|    ## - | ||||
|    info_msg="save PostgreSQL's Roles" | ||||
|    echononl "\t$info_msg" | ||||
|  | ||||
|    ## - begin timestamp | ||||
|    ## - | ||||
|    b_timestamp=`$date +"%s"` | ||||
|  | ||||
|    filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       $( | ||||
|          $ssh $ssh_options ${ssh_user}@$srcHost "$sudo -u $pgsql_user $pg_dumpall -g -c " \ | ||||
|             > ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql 2> $err_Log | ||||
|          exit $? | ||||
|       ) | ||||
|       retval=$? | ||||
|    else | ||||
|       $( | ||||
|          $su $pgsql_user -c "$pg_dumpall -g -c" \ | ||||
|             > ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql 2> $err_Log | ||||
|          exit $? | ||||
|       ) | ||||
|       retval=$? | ||||
|    fi | ||||
|    err_msg="Cannot save PostgreSQL's Roles" | ||||
|    keep_backup_on_error=false | ||||
|  | ||||
|    if [ "$retval" = 0 ];then | ||||
|       [ -z $pgsql_gzip ] && pgsql_gzip=false | ||||
|       if $pgsql_gzip ; then | ||||
|          $gzip ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql 2> $err_Log | ||||
|          retval=$? | ||||
|          err_msg="Cannot gzip PostreSQL_Roles-${filedate}.sql" | ||||
|          keep_backup_on_error=true | ||||
|       fi | ||||
|    fi | ||||
|  | ||||
|    ## - 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 | ||||
|       echolog "" | ||||
|       echolog "\t[ERROR] ${err_msg}  [ $duration ]\n\t`$cat $err_Log`\n" | ||||
|       if ! $keep_backup_on_error ; then | ||||
|          rm -f ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql | ||||
|       fi | ||||
|    else | ||||
|  | ||||
|       ## - print durations right-aligned | ||||
|       ## - | ||||
|       [ -z $right_tabstop ] && right_tabstop=65 | ||||
|       _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 "$_str_blanks" | ||||
|  | ||||
|       echolog "  [ $duration ]" | ||||
|    fi | ||||
|  | ||||
|    ## - END: Backup PostgreSQL's Roles | ||||
|    ## --------------------------------- | ||||
|  | ||||
|  | ||||
|    ## ----------------------------------- | ||||
|    ## - Create Database Creation SQL file | ||||
|    ## - | ||||
|    info_msg="create Database Creation SQL file" | ||||
|    echononl "\t$info_msg" | ||||
|  | ||||
|    ## - begin timestamp | ||||
|    ## - | ||||
|    b_timestamp=`$date +"%s"` | ||||
|  | ||||
|    filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       $( | ||||
|          $ssh $ssh_options ${ssh_user}@$srcHost "$sudo -u $pgsql_user $pg_dumpall -s " | \ | ||||
|             grep -e "^CREATE DATABASE" \ | ||||
|             > ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql 2> $err_Log | ||||
|          exit $? | ||||
|       ) | ||||
|       retval=$? | ||||
|    else | ||||
|       $( | ||||
|          $su - $pgsql_user -c "$pg_dumpall -s" | \ | ||||
|             grep -e "^CREATE DATABASE" \ | ||||
|             > ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql 2> $err_Log | ||||
|          exit $? | ||||
|       ) | ||||
|       retval=$? | ||||
|    fi | ||||
|    err_msg="Cannot create Database Creation SQL file" | ||||
|    keep_backup_on_error=false | ||||
|  | ||||
|    if [ "$retval" = 0 ];then | ||||
|       [ -z $pgsql_gzip ] && pgsql_gzip=false | ||||
|       if $pgsql_gzip ; then | ||||
|          $gzip ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql 2> $err_Log | ||||
|          retval=$? | ||||
|          err_msg="Cannot gzip PostreSQL_Databases-${filedate}.sql" | ||||
|          keep_backup_on_error=true | ||||
|       fi | ||||
|    fi | ||||
|  | ||||
|    ## - 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 | ||||
|       echolog "" | ||||
|       echolog "\t[ERROR] ${err_msg}  [ $duration ]\n\t`$cat $err_Log`\n" | ||||
|       if ! $keep_backup_on_error ; then | ||||
|          rm -f ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql | ||||
|       fi | ||||
|    else | ||||
|  | ||||
|       ## - print durations right-aligned | ||||
|       ## - | ||||
|       [ -z $right_tabstop ] && right_tabstop=65 | ||||
|       _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 "$_str_blanks" | ||||
|  | ||||
|       echolog "  [ $duration ]" | ||||
|    fi | ||||
|  | ||||
|    ## - End: Create Database Creation SQL file | ||||
|    ## ---------------------------------------- | ||||
|  | ||||
|  | ||||
|    ## - backup all databases.. | ||||
|    ## - | ||||
|    for i in $DATABASES ; do | ||||
|       if [ "$i" = "template0" ]; then | ||||
|          continue  | ||||
|       fi | ||||
|       if [ "$i" = ":" -o "$i" = "|" ]; then | ||||
|          continue | ||||
|       fi | ||||
|       filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|       ## - begin timestamp | ||||
|       ## - | ||||
|       b_timestamp=`$date +"%s"` | ||||
|  | ||||
|       if [ "$destHost" = "localhost" ]; then | ||||
|          if [ ! -d ${_backupDestArchiveDir}/${i} ] ; then  | ||||
|             $mkdir -p ${_backupDestArchiveDir}/${i} | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       info_msg="save database $i.." | ||||
|       echononl "\t$info_msg" | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $( | ||||
|             $ssh $ssh_options ${ssh_user}@$srcHost "$sudo -u $pgsql_user $pg_dump  -c  $i" \ | ||||
|                > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       else | ||||
|          $( | ||||
|             $su - $pgsql_user -c "$pg_dump  -c  $i" \ | ||||
|                > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       fi | ||||
|       err_msg="Cannot save database \"$i\"" | ||||
|       keep_backup_on_error=false | ||||
|  | ||||
|       if [ "$retval" = 0 ];then | ||||
|          [ -z $pgsql_gzip ] && pgsql_gzip=false | ||||
|          if $pgsql_gzip ; then | ||||
|             $gzip ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log | ||||
|             retval=$? | ||||
|             err_msg="Cannot gzip database \"$i\"" | ||||
|             keep_backup_on_error=true | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       ## - 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 | ||||
|          echolog "" | ||||
|          echolog "\t[ERROR] ${err_msg}  [ $duration ]\n\t`$cat $err_Log`\n" | ||||
|          if ! $keep_backup_on_error ; then | ||||
|             rm -f ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql | ||||
|          fi | ||||
|          continue | ||||
|       else | ||||
|  | ||||
|  | ||||
|          ## - print durations right-aligned | ||||
|          ## - | ||||
|          [ -z $right_tabstop ] && right_tabstop=65 | ||||
|          _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 "$_str_blanks" | ||||
|  | ||||
|          echolog "  [ $duration ]" | ||||
|       fi | ||||
|  | ||||
|    done | ||||
|     | ||||
|    if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|       $ssh ${ssh_user}@$srcHost "$sudo -u $pgsql_user $psql  -lt" > ${_backupDestArchiveDir}/database-list_${filedate}.txt | ||||
|    else | ||||
|       $su - $pgsql_user -c "$psql  -lt" > ${_backupDestArchiveDir}/database-list_${filedate}.txt | ||||
|    fi | ||||
|  | ||||
| else | ||||
|    echolog "\tARCHIVE is set to $ARCHIVE. So nothing to do."    | ||||
| fi | ||||
| echolog "" | ||||
|  | ||||
| ## - remove all files older than $days days.. | ||||
| ## -  | ||||
| $find $_backupDestArchiveDir -type f -mtime +${days} -exec rm {} \; | ||||
							
								
								
									
										187
									
								
								hosts/scripts/svn_backup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										187
									
								
								hosts/scripts/svn_backup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,187 @@ | ||||
| #!/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 | ||||
| ## - | ||||
| ## - on the remote database allow the congigured mysql user to | ||||
| ## - access the database from localhost an give him the follwing | ||||
| ## - privileges: | ||||
| ## -    Select_priv | ||||
| ## -    Lock_tables_priv | ||||
| ## -    Show_view_priv | ||||
| ## -  | ||||
| ## - INSERT INTO user (Host,User,Password,Select_priv,Lock_tables_priv,Show_view_priv) VALUES('localhost','backup',password('backup'),'Y','Y','Y'); | ||||
| ## - FLUSH PRIVILEGES; | ||||
| ## -  | ||||
| ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||||
|  | ||||
| err_Log=${LOCK_DIR}/svn.err.log | ||||
| > $err_Log | ||||
|  | ||||
|  | ||||
| ## - load functions | ||||
| ## - | ||||
| . $rcopy_functions_file | ||||
|  | ||||
| # --------------------------------------------------- # | ||||
| # -------------------- Variable --------------------- # | ||||
| # | ||||
|  | ||||
| _backupDestArchiveDir="${script_backup_dir}/Subversion" | ||||
|  | ||||
|  | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    svnadmin_bin=`$ssh ${ssh_user}@$srcHost which svnadmin` | ||||
|    find=`$ssh ${ssh_user}@$srcHost which find` | ||||
|    sudo=`$ssh ${ssh_user}@$srcHost which sudo` | ||||
|    realpath=`$ssh ${ssh_user}@$srcHost which realpath` | ||||
|    ssh_options="-o BatchMode=yes -o ConnectTimeout=360" | ||||
| else | ||||
|    svnadmin_bin=`which svnadmin` | ||||
| fi | ||||
|  | ||||
| if [ -z "$svnadmin_bin" ]; then | ||||
|   fatal "cannot find binary \"svnadmin\""  | ||||
|     | ||||
| fi | ||||
|  | ||||
|  | ||||
| # | ||||
| # ------------------ Ende Variable ------------------ # | ||||
| # --------------------------------------------------- # | ||||
|  | ||||
| if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|    _REPOSITORIES=`ssh ${ssh_user}@$srcHost "$sudo $find $svn_source_base_path -maxdepth 1 -type d -exec basename 'realpath {}' \;"` | ||||
| else | ||||
|    _REPOSITORIES=`find $svn_source_base_path -maxdepth 1 -type d -exec basename 'realpath {}' \;` | ||||
| fi | ||||
|  | ||||
| for repos in $_REPOSITORIES ; do | ||||
|    _dir=`basename $svn_source_base_path` | ||||
|    [ "$repos" = "$_dir" ] && continue | ||||
|    REPOSITORIES="$REPOSITORIES $repos" | ||||
| done | ||||
|     | ||||
|  | ||||
| echolog "" | ||||
| if $ARCHIVE ;then | ||||
|  | ||||
|    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 | ||||
|  | ||||
|  | ||||
|    ## - backup all svn repositories. | ||||
|    ## - | ||||
|    for i in $REPOSITORIES ; do | ||||
|       filedate=`$date +"%Y-%m-%d-%H%M"` | ||||
|  | ||||
|       if [ "$destHost" = "localhost" ]; then | ||||
|          if [ ! -d ${_backupDestArchiveDir}/${i} ] ; then  | ||||
|             $mkdir -p ${_backupDestArchiveDir}/${i} | ||||
|          fi | ||||
|       fi | ||||
|     | ||||
|       ## - begin timestamp | ||||
|       ## - | ||||
|       b_timestamp=`$date +"%s"` | ||||
|  | ||||
|       info_msg="save svn repository $i.." | ||||
|       echononl "\t$info_msg" | ||||
|       if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then | ||||
|          $( | ||||
|             $ssh $ssh_options ${ssh_user}@$srcHost "$sudo $svnadmin_bin dump $svn_source_base_path/$i 2> /dev/null" \ | ||||
|                > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       else | ||||
|          $( | ||||
|             $sudo $svnadmin_bin dump $svn_source_base_path/$i 2> /dev/null \ | ||||
|                > ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn 2> $err_Log | ||||
|             exit $? | ||||
|          ) | ||||
|          retval=$? | ||||
|       fi | ||||
|       err_msg="Cannot save repository \"$i\"" | ||||
|       keep_backup_on_error=false | ||||
|  | ||||
|       if [ "$retval" = 0 ];then | ||||
|          [ -z $svn_gzip ] && svn_gzip=false | ||||
|          if $svn_gzip ; then | ||||
|             $gzip ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn 2> $err_Log | ||||
|             retval=$? | ||||
|             err_msg="Cannot gzip repository \"$i\"" | ||||
|             keep_backup_on_error=true | ||||
|          fi | ||||
|       fi | ||||
|  | ||||
|       ## - 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 | ||||
|          echolog "" | ||||
|          echolog "\t[ERROR] ${err_msg}  [ $duration ]\n\t`$cat $err_Log`\n" | ||||
|          if ! $keep_backup_on_error ; then | ||||
|             rm -f ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn | ||||
|          fi | ||||
|          continue | ||||
|       else | ||||
|  | ||||
|          ## - print durations right-aligned | ||||
|          ## - | ||||
|          [ -z $right_tabstop ] && right_tabstop=65 | ||||
|          _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 "$_str_blanks" | ||||
|  | ||||
|          echolog "  [ $duration ]" | ||||
|       fi | ||||
|    done | ||||
|  | ||||
| else | ||||
|    echolog "\tARCHIVE is set to $ARCHIVE. So nothing to do."    | ||||
| fi | ||||
| echolog "" | ||||
|  | ||||
|  | ||||
| ## - remove all files older than $days days.. | ||||
| ## -  | ||||
| $find $_backupDestArchiveDir -type f -mtime +${days} -exec rm {} \; | ||||
		Reference in New Issue
	
	Block a user