github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/redis/dataprotection/backup.sh (about)

     1  set -o pipefail
     2  
     3  # if the script exits with a non-zero exit code, touch a file to indicate that the backup failed,
     4  # the sync progress container will check this file and exit if it exists
     5  function handle_exit() {
     6      exit_code=$?
     7      if [ $exit_code -ne 0 ]; then
     8          echo "failed with exit code $exit_code"
     9          touch "${DP_BACKUP_INFO_FILE}.exit"
    10          exit 1
    11      fi
    12  }
    13  trap handle_exit EXIT
    14  
    15  export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
    16  export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
    17  connect_url="redis-cli -h ${DP_DB_HOST} -p ${DP_DB_PORT} -a ${DP_DB_PASSWORD}"
    18  last_save=$(${connect_url} LASTSAVE)
    19  echo "INFO: start BGSAVE"
    20  ${connect_url} BGSAVE
    21  echo "INFO: wait for saving rdb successfully"
    22  while true; do
    23    end_save=$(${connect_url} LASTSAVE)
    24    if [ $end_save -ne $last_save ];then
    25       break
    26    fi
    27    sleep 1
    28  done
    29  echo "INFO: start to save data file..."
    30  cd ${DATA_DIR}
    31  # NOTE: if files changed during taring, the exit code will be 1 when it ends.
    32  tar -czvf - ./ | datasafed push - "${DP_BACKUP_NAME}.tar.gz"
    33  echo "INFO: save data file successfully"
    34  TOTAL_SIZE=$(datasafed stat / | grep TotalSize | awk '{print $2}')
    35  echo "{\"totalSize\":\"$TOTAL_SIZE\"}" > "${DP_BACKUP_INFO_FILE}" && sync