Created
December 5, 2023 14:57
-
-
Save podlom/be850a17d1f9f8279a1fb093064dd8b4 to your computer and use it in GitHub Desktop.
Start stage db backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set up local and remote credentials | |
LOCAL_WP_PATH="/nas/content/live/asianlegacystg" # Change to your WordPress directory path | |
LOCAL_UPLOADS_PATH="${LOCAL_WP_PATH}/wp-content/uploads" # Local uploads directory path | |
DB_BACKUP_PATH="${LOCAL_WP_PATH}/wp-content/_db_backups" # Local backup directory | |
DB_BACKUP_NAME="_wp_stage_backup_$(date +%F_%H-%M-%S).sql" # Backup file name with a timestamp | |
TAR_BACKUP_NAME="${DB_BACKUP_NAME}.tar.bz2" # Tar Backup file name with a timestamp | |
# Make sure the local db backups directory exists | |
if [ -d "${DB_BACKUP_PATH}" ]; then | |
# Display db backups directory | |
printf "Display directory listing in ${DB_BACKUP_PATH}.\n" | |
ls -alh "${DB_BACKUP_PATH}" | |
# Remove all SQL backup files in folder | |
rm -fv "${DB_BACKUP_PATH}/*.sql" | |
else | |
# Make sure the local db backups directory exists | |
echo "Directory does not exist. Creating directory ${DB_BACKUP_PATH}..." | |
mkdir -pv "${DB_BACKUP_PATH}" | |
fi | |
# Navigate to the WordPress installation directory | |
cd "${LOCAL_WP_PATH}" | |
# Print working directory | |
pwd | |
# Display current directory listing | |
ls -alh | |
# Create a backup of the WordPress database using WP-CLI | |
wp db export "${DB_BACKUP_PATH}/${DB_BACKUP_NAME}" | |
# Check if WP-CLI successfully created the backup | |
if [ -f "${DB_BACKUP_PATH}/${DB_BACKUP_NAME}" ]; then | |
printf "Database backup was created successfully.\n" | |
# Make tar archive of SQL file | |
tar cpjvf "${DB_BACKUP_PATH}/${TAR_BACKUP_NAME}" "${DB_BACKUP_PATH}/${DB_BACKUP_NAME}" | |
echo "Well done at:" | |
date | |
# Show local db backup file listing | |
ls "${DB_BACKUP_PATH}/${TAR_BACKUP_NAME}" | |
else | |
echo "Failed to create stage database backup." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment