This commit is contained in:
hhftechnologies 2024-11-27 15:36:47 +05:30
parent e74573ce03
commit 3002a9b627

240
backup/wp_backup.sh Normal file
View file

@ -0,0 +1,240 @@
#!/bin/bash
#-----------------------------------#
# VARIABLES #
#-----------------------------------#
this_script_url=https://git.hhf.technology/hhf/script-management-cloudpanel/raw/branch/main/backup/wp_backup.sh"
this_script_name="WordPress Backup Script"
formatter_url="https://git.hhf.technology/hhf/TaskFormatter/raw/branch/main/bash_task_formatter/task_formatter.sh"
scriptname=$0
# Initialize success flag
success=0
# Determine the user (use the first argument if provided, otherwise fallback)
USER_TO_RUN_AS="${1:-$SUDO_USER}"
USER_HOME=$(eval echo ~$USER_TO_RUN_AS)
# Function to collect WordPress and backup variables
collect_variables() {
echo "Please provide the following information:"
echo "----------------------------------------"
# Database name
read -p "Enter database name: " DATABASE
# WordPress directory
read -p "Enter WordPress installation path (e.g., /home/username/htdocs/domain.com): " WP_DIR
# Derive WordPress content directories
UPLOADS_DIR="${WP_DIR}/wp-content/uploads"
THEMES_DIR="${WP_DIR}/wp-content/themes"
PLUGINS_DIR="${WP_DIR}/wp-content/plugins"
WPCONTENT_DIR="${WP_DIR}/wp-content"
echo -e "\nBackup Locations:"
# Scripts directory
read -p "Enter scripts directory path (e.g., /home/username/scripts): " SCRIPTS_DIR
# Backup directory
BACKUP_DIR="${SCRIPTS_DIR}/backups"
echo "Backup directory will be: ${BACKUP_DIR}"
echo -e "\nS3 Configuration:"
# S3cmd path
S3_CMD="/usr/local/bin/s3cmd"
if [ ! -f "$S3_CMD" ]; then
read -p "s3cmd not found at default location. Enter s3cmd path: " S3_CMD
fi
# S3 bucket information
read -p "Enter S3 bucket name (e.g., my-bucket): " S3_BUCKET_NAME
read -p "Enter S3 folder name (e.g., backups): " S3_FOLDER_NAME
S3_BUCKET="s3://${S3_BUCKET_NAME}/${S3_FOLDER_NAME}/"
# Current date for backup naming
CURRENT_DATE=$(date +"%Y-%m-%d")
# Display collected information
echo -e "\nSummary of collected information:"
echo "----------------------------------------"
echo "Database Name: $DATABASE"
echo "WordPress Directory: $WP_DIR"
echo "Scripts Directory: $SCRIPTS_DIR"
echo "Backup Directory: $BACKUP_DIR"
echo "S3 Bucket Path: $S3_BUCKET"
# Confirm information
read -p "Is this information correct? (y/n): " confirm
if [[ $confirm != [Yy]* ]]; then
echo "Please run the script again with correct information."
exit 1
fi
return 0
}
#-----------------------------------#
# FORMATTER #
#-----------------------------------#
# Download and source the formatter with error handling
download_formatter() {
if [ ! -f "task_formatter.sh" ]; then
if ! wget "$formatter_url" --no-check-certificate -O task_formatter.sh > /dev/null 2>&1; then
echo "Error: Failed to download task_formatter.sh"
exit 1
fi
fi
if [ ! -f "task_formatter.sh" ]; then
echo "Error: task_formatter.sh not found after download attempt"
exit 1
fi
chmod +x task_formatter.sh
source ./task_formatter.sh || { echo "Error: Failed to source task_formatter.sh"; exit 1; }
if ! declare -f print_header > /dev/null; then
echo "Error: print_header function not found after sourcing."
exit 1
fi
}
# Call the download_formatter function
download_formatter
#-----------------------------------#
# FUNCTIONS #
#-----------------------------------#
# Function to check WordPress installation
check_wordpress() {
if [ ! -d "${WP_DIR}" ]; then
echo "Directory ${WP_DIR} does not exist"
return 1
fi
if [ ! -f "${WP_DIR}/wp-config.php" ]; then
echo "No wp-config.php in ${WP_DIR}"
return 1
fi
echo "Found wp-config.php in ${WP_DIR}"
return 0
}
# Function to prepare backup directory
prepare_backup_dir() {
mkdir -p "$BACKUP_DIR"
rm -rf "${BACKUP_DIR:?}/*"
echo "Created and cleaned ${BACKUP_DIR}"
return 0
}
# Function to backup database
backup_database() {
echo "Creating Database dump..."
if ! clpctl db:export --databaseName="${DATABASE}" --file="$BACKUP_DIR/${DATABASE}.sql.gz"; then
echo "Database backup failed"
return 1
fi
echo "Database dump created at ${BACKUP_DIR}/${DATABASE}.sql.gz"
return 0
}
# Function to backup WordPress files
backup_wordpress_files() {
echo "Creating WordPress file backups..."
# Backup WordPress core files
tar -cjf "${BACKUP_DIR}/wp_files.tar.bz2" --exclude='wp-content' "${WP_DIR}"
# Backup wp-content excluding specific directories
tar -cjf "${BACKUP_DIR}/others.tar.bz2" \
--exclude='themes' --exclude='plugins' --exclude='uploads' \
--exclude='cache' --exclude='updraft' --exclude='backups-dup-pro' \
--exclude='backups-dup' --exclude='ai1wm-backups' "${WPCONTENT_DIR}"
# Backup specific directories
tar -cjf "${BACKUP_DIR}/uploads.tar.bz2" "${UPLOADS_DIR}"
tar -cjf "${BACKUP_DIR}/themes.tar.bz2" "${THEMES_DIR}"
tar -cjf "${BACKUP_DIR}/plugins.tar.bz2" "${PLUGINS_DIR}"
return 0
}
# Function to upload to S3
upload_to_s3() {
mv "${BACKUP_DIR}" "${SCRIPTS_DIR}/${CURRENT_DATE}"
echo "Uploading backup to S3..."
if ! $S3_CMD sync "${SCRIPTS_DIR}/${CURRENT_DATE}" "${S3_BUCKET}"; then
echo "S3 upload failed"
return 1
fi
return 0
}
# Function to clean up
cleanup() {
echo "Removing local files..."
rm -rf "${SCRIPTS_DIR:?}/${CURRENT_DATE}"
return 0
}
#-----------------------------------#
# MAIN LOGIC #
#-----------------------------------#
# Check if print_header function exists
if ! command -v print_header >/dev/null 2>&1; then
echo "Error: print_header function not found. Formatter may not be properly sourced."
exit 1
fi
# Print header
print_header "$this_script_name" "$this_script_url"
echo -e "Running as User: $USER_TO_RUN_AS\nUser Home: $USER_HOME\n"
# Collect variables from user
if ! format_output collect_variables "Collecting Configuration Information"; then
success=1
exit $success
fi
# Run the backup functions with formatted output
if ! format_output check_wordpress "Checking WordPress Installation"; then
success=1
exit $success
fi
if ! format_output prepare_backup_dir "Preparing Backup Directory"; then
success=1
exit $success
fi
if ! format_output backup_database "Backing up Database"; then
success=1
exit $success
fi
if ! format_output backup_wordpress_files "Backing up WordPress Files"; then
success=1
exit $success
fi
if ! format_output upload_to_s3 "Uploading to S3"; then
success=1
exit $success
fi
if ! format_output cleanup "Cleaning up temporary files"; then
success=1
exit $success
fi
# Print final message
final_message "$this_script_name" "$success"
# Exit with appropriate status
exit $success