Skip to content
Snippets Groups Projects
rsync.in 30.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • rhatto's avatar
    rhatto committed
    function mount_ro {
    
      # remount backup destination as read-only
    
      if [ "$dest" == "local" ]; then
        if [ "$read_only" == "1" ] || [ "$read_only" == "yes" ]; then
          mount -o remount,ro $mountpoint
        fi
      fi
    
    }
    
    rhatto's avatar
    rhatto committed
    function run_fsck {
    
    rhatto's avatar
    rhatto committed
      # check partition for errors
    
      if [ "$dest" == "local" ]; then
        if [ "$fscheck" == "1" ] || [ "$fscheck" == "yes" ]; then
          umount $mountpoint
    
          if (($?)); then
    
    rhatto's avatar
    rhatto committed
            warning "Could not umount $mountpoint to run fsck"
          else
            $nice $fsck -v -y $partition >> $log
            mount $mountpoint
    
    rhatto's avatar
    rhatto committed
    function include_vservers {
    
    rhatto's avatar
    rhatto committed
      # add vservers to included folders
    
    rhatto's avatar
    rhatto committed
      if [ "$vservers_are_available" == "yes" ]; then
    
    rhatto's avatar
    rhatto committed
        # sane permission on backup
        mkdir -p $backupdir/$VROOTDIR
        chmod 000 $backupdir/$VROOTDIR
    
        for candidate in $found_vservers; do
    
          candidate="`basename $candidate`"
          found_excluded_vserver="0"
          for excluded_vserver in $exclude_vserver; do
    
    rhatto's avatar
    rhatto committed
            if [ "$excluded_vserver" == "$candidate" ]; then
              found_excluded_vserver="1"
              break
            fi
    
          done
          if [ "$found_excluded_vserver" == "0" ]; then
    
    rhatto's avatar
    rhatto committed
            include="$include $VROOTDIR/$candidate"
    
    rhatto's avatar
    rhatto committed
        done
      fi
    
    rhatto's avatar
    rhatto committed
    }
    
    rhatto's avatar
    rhatto committed
    function start_mux {
    
    rhatto's avatar
    rhatto committed
      if [ "$multiconnection" == "yes" ]; then
        debug "Starting master ssh connection"
        $ssh_cmd -M sleep 1d &
        sleep 1
      fi
    
    rhatto's avatar
    rhatto committed
    }
    
    rhatto's avatar
    rhatto committed
    function end_mux {
    
    rhatto's avatar
    rhatto committed
      if [ "$multiconnection" == "yes" ]; then
        debug "Stopping master ssh connection"
        $ssh_cmd pkill sleep
      fi
    
    rhatto's avatar
    rhatto committed
    }
    
    function set_pipefail {
    
      # Save initial pipefail status for later restoration
      if echo "$SHELLOPTS" | grep -q ":pipefail"; then
         pipefail="-o"
      else
         pipefail="+o"
      fi
    
      # Ensure that a non-zero rsync exit status is caught by our handler
      set -o pipefail
    
    }
    
    function restore_pipefail {
    
      if [ ! -z "$pipefail" ]; then
        set $pipefail pipefail
      fi
    
    }
    
    
    function check_rsync_exit_status {
    
      if [ -z "$1" ]; then
        return
      fi
    
      case $1 in
    
        1|2|3|4|5|6|10|11|12|13|14|21)
           fatal "Rsync error $1 when trying to transfer $SECTION"
           ;;
        *)
           warning "Rsync error $1 when trying to transfer $SECTION"
           ;;
      esac
    
    }
    
    
    rhatto's avatar
    rhatto committed
    # the backup procedure
    
    rhatto's avatar
    rhatto committed
    eval_config
    set_rsync_options
    start_mux
    stop_services
    mount_rw
    
    echo "Starting backup at `echo "$starttime" | head -n 1`" >> $log
    
    rhatto's avatar
    rhatto committed
    for SECTION in $include; do
    
    rhatto's avatar
    rhatto committed
      prepare_storage
      set_orig
      set_batch_mode
      set_filelist
      set_dest
    
    rhatto's avatar
    rhatto committed
      info "Syncing $SECTION on $dest_path..."
    
      debug $nice $rsync ${rsync_options[@]} $filelist_flag $excludes $batch_option $orig $dest_path
    
      $nice su -c "$rsync ${rsync_options[@]} --delete-excluded $filelist_flag $excludes $batch_option $orig $dest_path" | tee -a $log
    
      check_rsync_exit_status $?
    
      restore_pipefail
    
    rhatto's avatar
    rhatto committed
      update_metadata
    
    rhatto's avatar
    rhatto committed
    done
    
    rhatto's avatar
    rhatto committed
    mount_ro
    run_fsck
    start_services
    end_mux
    
    
    echo "Finnishing backup at `date`" >> $log