30
  1   ˆl–ßi~” T¾R( 	Å�¿q·Å£�þ@³Ž÷‰(?çÌ0ËÀÈ      ˆ¿‘äÇò 8à_}Ð”,ÿ?Ìƒh¹ËÀÈ      none
      ˆ¿‘äÇò 8à_}ç›uÏçÌƒh¹ËÀÈ      input:b0011v0002p0013e0006-e0,1,2,k110,111,r0,1,8,amlsfw
  7   ˆl–ß=¿Jå2ÛR‚ œPA¸&n41hß�þZ�,ãm=!ÓLÆG¿ŸÍ„�6/ÌÁÉ ?÷     #!/usr/bin/env bash
#
# Zammad backup script functions
#

function demand_backup_conf () {
  if [ -f "${BACKUP_SCRIPT_PATH}/config" ]; then
    # Ensure we're inside of our Backup-Script folder (see issue 2508)
    # shellcheck disable=SC2164
    cd "${BACKUP_SCRIPT_PATH}"

    # import config
    . ${BACKUP_SCRIPT_PATH}/config
  else
    echo -e "\n The 'config' file is missing!"
    echo -e " Please copy ${BACKUP_SCRIPT_PATH}/config.dist to  ${BACKUP_SCRIPT_PATH}/config before running $0!\n"
    echo -e " Learn more about the backup configuration at https://docs.zammad.org/en/latest/appendix/backup-and-restore/configuration.html"
    exit 1
  fi

  # Check if filesystem full dump setting exists and fall back if not
  if [ -z ${FULL_FS_DUMP+x} ]; then
    # Falling back to old default behavior
    FULL_FS_DUMP='yes'

    if [ "${DEBUG}" == "yes" ]; then
      echo "FULL_FS_DUMP is not set, falling back to 'yes' to produce a full backup."
    fi
  fi

  # set VARs related to pigz
  if command -v pigz  > /dev/null; then
    if [ "${DEBUG}" == "yes" ]; then
      echo "Using pigz instead of gzip."
    fi
    GZIP_PROG="pigz"
    TAR_GZIP_ARG="-I $GZIP_PROG"
  else
    if [ "${DEBUG}" == "yes" ]; then
      echo "pigz is not available, falling back to gzip."
    fi
    GZIP_PROG="gzip"
    TAR_GZIP_ARG="-z"
  fi
}

function get_zammad_dir () {
  ZAMMAD_DIR="$(echo ${BACKUP_SCRIPT_PATH} | sed -e 's#/contrib/backup.*##g')"
}

function get_backup_date () {
  TIMESTAMP="$(date +'%Y%m%d%H%M%S')"

  if [ "${DEBUG}" == "yes" ]; then
    echo "timestamp is ${TIMESTAMP}"
  fi
}

function delete_old_backups () {
  # Use -mmin to clean up files as -mtime (days) is too unprecise.
  #   However, add +60 minutes to allow for the backup script run time, so that
  #   backups created a few minutes more than a day ago are not already purged.
  FILES_TO_DELETE=$(test -d "${BACKUP_DIR}" && find "${BACKUP_DIR}" -maxdepth 1 -type f -name '*_zammad_*.gz' -mmin "+$((60*24*HOLD_DAYS+60))")

  if [[ -n ${FILES_TO_DELETE} ]]; then
    if [ "${DEBUG}" == "yes" ]; then
      echo "Deleting old backups according to value set in HOLD_DAYS."
    fi
    echo "${FILES_TO_DELETE}" | xargs rm
  else
    if [ "${DEBUG}" == "yes" ]; then
      echo "No files found matching value set in HOLD_DAYS. No files deleted."
    fi
  fi
}

function get_db_credentials () {
  DB_HOST="$(grep -m 1 '^[[:space:]]*host:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*host:[[:space:]]*//g')"
  DB_PORT="$(grep -m 1 '^[[:space:]]*port:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*port:[[:space:]]*//g')"
  DB_NAME="$(grep -m 1 '^[[:space:]]*database:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*database:[[:space:]]* //g')"
  DB_USER="$(grep -m 1 '^[[:space:]]*username:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*username:[[:space:]]*//g')"
  DB_PASS="$(grep -m 1 '^[[:space:]]*password:' < ${ZAMMAD_DIR}/config/database.yml | sed -e 's/.*password:[[:space:]]*//g')"

  # Ensure that HOST and PORT are not empty, provide defaults if needed.
  if [ "${DB_HOST}x" == "x" ]; then
    DB_HOST="localhost"
  fi
  if [ "${DB_PORT}x" == "x" ]; then
    DB_PORT="5432"
  fi

  if [ "${DEBUG}" == "yes" ]; then
    echo "dbhost=${DB_HOST} dbport=${DB_PORT} dbname=${DB_NAME} dbuser=${DB_USER} dbpass=${DB_PASS}"
  fi
}

function backup_dir_create () {
  test -d ${BACKUP_DIR} || mkdir -p ${BACKUP_DIR}

  state=$?

  if [ "${state}" == "1" ]; then
    echo -e "\n\n # ERROR(${state}) - Creation of backup directory failed. Please double check permissions."
    echo -e " #-> BACKUP WAS NOT SUCCESSFUL"
    exit 3
  fi

  if [ "${DEBUG}" == "yes" ]; then
    echo "backup dir is ${BACKUP_DIR}"
  fi
}

backup_file_write_test () {
  # We're testing if we can actually write into the provided direct