#!/usr/local/bin/bash
#/ Usage: ghe-backup-mysql-binary <host>
#/ Backup MySQL from a GitHub instance using binary backup strategy.
#/
#/ Note: This script typically isn't called directly. It's invoked by the
#/ ghe-backup command.
set -e

# Bring in the backup configuration
# shellcheck source=share/github-backup-utils/ghe-backup-config
. "$(dirname "${BASH_SOURCE[0]}")/ghe-backup-config"

bm_start "$(basename $0)"
log_info "Backing up MySQL database using binary backup strategy ..."

cleanup() {
    hard_limit=$(ulimit -Hf)
    # MySQL backups produce bigger single files, prone to file size limits.
    # This does not show on ssh streaming, only in broken pipe messages. So warn again here.
    if [ "$hard_limit" != "unlimited" ]; then
        log_warn_verbose "Hard limit for file size is set to $hard_limit blocks. Please ensure the limit is set higher than the mysql backup file."
    fi
    # Saves last 30 lines of xtrabackup log to the backup directory
    xtrabackup_log=$(ghe-ssh "$GHE_HOSTNAME" -- "tail -n 30 /var/log/xtrabackup/xtrabackup-export.log")
    echo "$xtrabackup_log" >"$GHE_SNAPSHOT_DIR/xtrabackup-export.log"
    log_info "Saved last 30 lines of xtrabackup log to xtrabackup-export.log"
}

trap_full_cleanup

# Perform a host-check and establish the remote version in GHE_REMOTE_VERSION.
ghe_remote_version_required "$GHE_HOSTNAME"

is_inc=$(is_incremental_backup_feature_on)
if [ $is_inc = true ]; then
    log_verbose "Incremental backups are configured on the target environment."
    log_info_verbose "Performing incremental backup of MySQL database ..."
    INC_TYPE=$(full_or_incremental_backup)
    INC_LSN=""
    if [ "$INC_TYPE" == "full" ]; then
        log_info_verbose "Incremental backup type: $INC_TYPE"
        INC_LSN=0 # 0 means full backup
    else
        validate_inc_snapshot_data
        log_info_verbose "Incremental backup type: $INC_TYPE"
        INC_LSN=$(retrieve_last_lsn)
    fi
    ghe-ssh "$GHE_HOSTNAME" -- "env INC_BACKUP=$INC_LSN ghe-export-mysql" >"$GHE_SNAPSHOT_DIR/mysql.sql.gz"
    touch "$GHE_SNAPSHOT_DIR/mysql-binary-backup-sentinel"
    # Ensure that we capture the xtrabackup_checkpoints file from the remote host
    log_info "Checking if incremental backup is part of a cluster"
    GET_LSN=$(get_cluster_lsn "$GHE_HOSTNAME")
    ghe-ssh "$GHE_HOSTNAME" "$GET_LSN" >"$GHE_SNAPSHOT_DIR/xtrabackup_checkpoints"
    if [ "$INC_TYPE" == "full" ]; then
        log_info_verbose "Adding $GHE_SNAPSHOT_DIR to the list of full backups"
        update_inc_full_backup "$GHE_SNAPSHOT_DIR"
    else
        log_info_verbose "Adding $GHE_SNAPSHOT_DIR to the list of incremental backups"
        update_inc_snapshot_data "$GHE_SNAPSHOT_DIR"
    fi
    bm_end "$(basename $0)"
    exit 0
fi
# if incremental backup isn't enabled, or we are performing a full backup as part of the process,
# fall through and do a full backup
ghe-ssh "$GHE_HOSTNAME" -- "ghe-export-mysql" >"$GHE_SNAPSHOT_DIR/mysql.sql.gz"
touch "$GHE_SNAPSHOT_DIR/mysql-binary-backup-sentinel"
is_inc=$(is_incremental_backup_feature_on)
if [ $is_inc = true ]; then
    update_inc_full_backup "$GHE_SNAPSHOT_DIR"
fi
bm_end "$(basename $0)"
