revision backup function
parent
d8c7209b7a
commit
290fd683af
|
@ -105,39 +105,49 @@ function create_snapshot(){
|
||||||
echo "$(date) ${ENV_NAME} End uploading the ${DUMP_NAME} snapshot to backup storage" | tee -a "${BACKUP_LOG_FILE}"
|
echo "$(date) ${ENV_NAME} End uploading the ${DUMP_NAME} snapshot to backup storage" | tee -a "${BACKUP_LOG_FILE}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function backup(){
|
function backup_database() {
|
||||||
local backup_repo_path="/mnt/backups/${ENV_NAME}"
|
local backup_repo_path="/mnt/backups/${ENV_NAME}/db"
|
||||||
|
local log_file="/home/litespeed/mbmanager/logs/${ENV_NAME}_db_backup.log"
|
||||||
|
|
||||||
|
# Ensure the backup and log directories exist
|
||||||
mkdir -p "${backup_repo_path}"
|
mkdir -p "${backup_repo_path}"
|
||||||
echo $$ > "/var/run/${ENV_NAME}_backup.pid"
|
mkdir -p "$(dirname "${log_file}")"
|
||||||
echo "$(date) ${ENV_NAME} Creating the ${BACKUP_TYPE} backup" | tee -a "${BACKUP_LOG_FILE}"
|
|
||||||
|
|
||||||
# Ensure Restic repository is initialized
|
echo "$(date) ${ENV_NAME} Starting database backup..." | tee -a "${log_file}"
|
||||||
restic -r "${backup_repo_path}" snapshots &>/dev/null || restic -r "${backup_repo_path}" init
|
|
||||||
|
|
||||||
# Extract database credentials
|
# Extract database credentials from wp-config.php
|
||||||
DB_NAME=$(grep DB_NAME /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
|
DB_NAME=$(grep DB_NAME /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
|
||||||
DB_USER=$(grep DB_USER /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
|
DB_USER=$(grep DB_USER /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
|
||||||
DB_PASSWORD=$(grep DB_PASSWORD /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
|
DB_PASSWORD=$(grep DB_PASSWORD /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
|
||||||
DB_HOST=$(grep DB_HOST /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4 | awk -F':' '{print $1}')
|
DB_HOST=$(grep DB_HOST /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4 | awk -F':' '{print $1}')
|
||||||
DB_PORT=$(grep DB_HOST /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4 | awk -F':' '{print $2}')
|
DB_PORT=$(grep DB_HOST /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4 | awk -F':' '{print $2}' | sed 's/)//')
|
||||||
DB_PORT=${DB_PORT:-3306}
|
DB_PORT=${DB_PORT:-3306}
|
||||||
|
|
||||||
echo "$(date) ${ENV_NAME} Creating the DB dump" | tee -a "${BACKUP_LOG_FILE}"
|
# Determine the correct --column-statistics option based on MySQL or MariaDB version
|
||||||
mysqldump -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASSWORD}" "${DB_NAME}" --force --single-transaction --quote-names --opt --databases > "${backup_repo_path}/wp_db_backup.sql"
|
local column_statistics_option=""
|
||||||
|
SERVER_VERSION=$(mysql -h "${DB_HOST}" -u "${DB_USER}" -p"${DB_PASSWORD}" -e 'SELECT VERSION();' -s -N)
|
||||||
|
if [[ "${SERVER_VERSION}" =~ ^5\.7|^8\.0|MariaDB ]]; then
|
||||||
|
column_statistics_option="--column-statistics=0"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create a database dump
|
||||||
|
local dump_file="${backup_repo_path}/${ENV_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").sql"
|
||||||
|
mysqldump -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASSWORD}" ${column_statistics_option} --force --single-transaction --quote-names --opt "${DB_NAME}" > "${dump_file}"
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "$(date) ${ENV_NAME} DB backup process failed." | tee -a "${BACKUP_LOG_FILE}"
|
echo "$(date) ${ENV_NAME} Database backup process failed." | tee -a "${log_file}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Backup process
|
# Backup the dump file using Restic
|
||||||
restic -r "${backup_repo_path}" backup "${backup_repo_path}/wp_db_backup.sql" "${APP_PATH}" --tag "${BACKUP_TYPE}" --host "${ENV_NAME}"
|
restic -r "${backup_repo_path}" backup "${dump_file}" --tag "db" --host "${ENV_NAME}"
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo "$(date) ${ENV_NAME} Backup process failed." | tee -a "${BACKUP_LOG_FILE}"
|
echo "$(date) ${ENV_NAME} Restic backup process failed." | tee -a "${log_file}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$(date) ${ENV_NAME} Backup process completed successfully." | tee -a "${BACKUP_LOG_FILE}"
|
echo "$(date) ${ENV_NAME} Database backup completed successfully." | tee -a "${log_file}"
|
||||||
rm -f "/var/run/${ENV_NAME}_backup.pid"
|
# Optionally, remove the dump file after successful backup
|
||||||
|
rm -f "${dump_file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function backup_wp_core() {
|
function backup_wp_core() {
|
||||||
|
|
Loading…
Reference in New Issue