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

     1  function get_current_time() {
     2    CLIENT=$(which mongosh >/dev/null && echo mongosh || echo mongo)
     3    curr_time=$(${CLIENT} -u ${DP_DB_USER} -p ${DP_DB_PASSWORD} --port 27017 --host ${DP_DB_HOST} --authenticationDatabase admin --eval 'db.isMaster().lastWrite.lastWriteDate.getTime()/1000' --quiet)
     4    curr_time=$(date -d "@${curr_time}" -u '+%Y-%m-%dT%H:%M:%SZ')
     5    echo $curr_time
     6  }
     7  
     8  function stat_and_save_backup_info() {
     9    export PATH="$PATH:$DP_DATASAFED_BIN_PATH"
    10    export DATASAFED_BACKEND_BASE_PATH="$DP_BACKUP_BASE_PATH"
    11    START_TIME=$1
    12    STOP_TIME=$2
    13    if [ -z $STOP_TIME ]; then
    14      STOP_TIME=$(get_current_time)
    15    fi
    16    TOTAL_SIZE=$(datasafed stat / | grep TotalSize | awk '{print $2}')
    17    echo "{\"totalSize\":\"$TOTAL_SIZE\",\"timeRange\":{\"start\":\"${START_TIME}\",\"end\":\"${STOP_TIME}\"}}" >"${DP_BACKUP_INFO_FILE}"
    18  }
    19  
    20  # if the script exits with a non-zero exit code, touch a file to indicate that the backup failed,
    21  # the sync progress container will check this file and exit if it exists
    22  function handle_exit() {
    23    exit_code=$?
    24    if [ $exit_code -ne 0 ]; then
    25      echo "failed with exit code $exit_code"
    26      touch "${DP_BACKUP_INFO_FILE}.exit"
    27      exit 1
    28    fi
    29  }