Skip to content

Instantly share code, notes, and snippets.

@gellweiler
Last active January 2, 2022 10:07
Show Gist options
  • Select an option

  • Save gellweiler/592da5ae543404211f9eb0a2bad00b27 to your computer and use it in GitHub Desktop.

Select an option

Save gellweiler/592da5ae543404211f9eb0a2bad00b27 to your computer and use it in GitHub Desktop.
Update-Script for CiviCRM running on Wordpress
#!/bin/bash
#
# Shell script to upgrade CiviCRM on Wordpres.
#
# Saves the existing database and directory.
# Then downloads and installs the new civicrm version & language pack.
# Finally updates the db.
# On error it will try to rollback to the state prior to updating.
#
# You will have to cleanup the files in $TEMP_DIR configured below for yourself
# after the update has been completed successfully.
#
# Do yourself a favor and start this script with nohup or better put it in a screen (or similiar) session.
# It can run for a very long time.
#
# Use with caution.
### Config-Section ###
# Version to upgrade to
CIVICRM_VERSION="${CIVICRM_VERSION:-5.41.0}"
# Directories
WP_DIR="${WP_DIR:-"/var/www/vhosts/XXX/httpdocs"}"
TEMP_DIR="${TEMP_DIR:-"/tmp/civicrm-update"}" # Used to store the backup, only delete these files after you have ensured the update was successfull.
# Civicrm credentials
CIVICRM_USER="${CVICRM_USER:-cronjob_user_for_civi_crm}"
CIVICRM_PASSWORD="${CIVICRM_PASSWORD:-XXX}"
# Database connection
CIVICRM_DB_NAME="${CIVICRM_DB_NAME:-XXX}"
CIVICRM_DB_USER="${CIVICRM_DB_USER:-XXX}"
CIVICRM_DB_PASSWORD="${CIVICRM_DB_PASSWORD:-XXX}"
CIVICRM_DB_HOST="${CIVICRM_DB_HOST:-localhost}"
CIVICRM_DB_PORT="${CIVICRM_DB_PORT:-3306}"
# On some systems the php binary may not be a php cli and you need to set a different path.
# For example on strato the path is /opt/RZphp73/bin/php-cli.
# See: https://blog.wappler.systems/php-cli-modus-auf-strato-servern/.
PHP_CLI_CMD="${PHP_CLI_CMD:-$(which php)}"
# Should we automatically rollback to the state prior to updating, if we detect an error during update?
# Possible values:
# - yes – always rollback on error
# - no – never rollback on error
# - ask – ask for confirmation before rolling back
ROLLBACK_ACTION="${ROLLBACK_ACTION:-ask}"
### Code-Section ###
set -euo pipefail
timestring="$(date +%Y-%m-%d_%H-%m-%S)"
civicrm_zip_path="${TEMP_DIR}/civicrm-${CIVICRM_VERSION}-wordpress.zip"
l10n_path="${TEMP_DIR}/civicrm-${CIVICRM_VERSION}-l10n.tar.gz"
wp_cli_path="${TEMP_DIR}/wp-cli.phar"
success_flag=0
function backup_db {
db_file_path="${TEMP_DIR}/civicrm-${timestring}.sql"
echo "[INFO] Backing up database to $db_file_path"
mysqldump \
--host="${CIVICRM_DB_HOST}" \
--port="${CIVICRM_DB_PORT}" \
--user="${CIVICRM_DB_USER}" \
--password="${CIVICRM_DB_PASSWORD}" \
--routines \
"${CIVICRM_DB_NAME}" \
> $db_file_path
echo "[INFO] Check that the db dump is at least 10 mb in size"
test "$(du -m $db_file_path | cut -f1)" -gt 10
echo "[INFO] Check that the db dump ends with Dump completed"
tail -n 1 $db_file_path | grep -i 'Dump completed'
}
function restore_db {
db_file_path="${TEMP_DIR}/civicrm-${timestring}.sql"
# Use the pipe viewer to display a progress bar if it is available
pipe_cmd="cat"
if command -v pv &> /dev/null; then
pipe_cmd="pv"
fi
echo "[INFO] Restoring database from $db_file_path"
$pipe_cmd $db_file_path | mysql \
--host="${CIVICRM_DB_HOST}" \
--port="${CIVICRM_DB_PORT}" \
--user="${CIVICRM_DB_USER}" \
--password="${CIVICRM_DB_PASSWORD}" \
--database="${CIVICRM_DB_NAME}"
}
function update_files {
old_files_path="${TEMP_DIR}/civicrm-${timestring}"
echo "[INFO] Moving civicrm files to ${old_files_path}"
mv "${WP_DIR}/wp-content/plugins/civicrm" "${old_files_path}"
echo "[INFO] Unpacking civicrm files"
unzip -q "$civicrm_zip_path" -d "${WP_DIR}/wp-content/plugins"
echo "[INFO] Unpacking language files"
tar -xf "$l10n_path" -C "${WP_DIR}/wp-content/plugins/civicrm"
# echo "[INFO] Restoring civicrm.config.php"
# cp "${old_files_path}/civicrm.settings.php" "${WP_DIR}/wp-content/plugins/civicrm/civicrm.settings.php"
}
function restore_files {
old_files_path="${TEMP_DIR}/civicrm-${timestring}"
if [ -d "${old_files_path}" ]; then
echo "[INFO] Restoring civicrm files from ${old_files_path}"
test -f "${WP_DIR}/wp-content/plugins/civicrm/civicrm.php" && rm -rf "${WP_DIR}/wp-content/plugins/civicrm"
cp -r "${old_files_path}" "${WP_DIR}/wp-content/plugins/civicrm"
fi
}
function rollback {
restore_db
restore_files
}
function wipe_caches {
echo "[INFO] Removing cache files"
rm -rf "${WP_DIR}/wp-content/plugins/files/civicrm/templates_c/"*
rm -rf "${WP_DIR}/wp-content/plugins/files/civicrm/ConfigAndLog/Config.IDS.ini"
echo "[INFO] Wiping cache tables"
mysql \
--host="${CIVICRM_DB_HOST}" \
--port="${CIVICRM_DB_PORT}" \
--user="${CIVICRM_DB_USER}" \
--password="${CIVICRM_DB_PASSWORD}" \
--database="${CIVICRM_DB_NAME}" \
<<EOF
TRUNCATE TABLE civicrm_acl_cache;
TRUNCATE TABLE civicrm_acl_contact_cache;
TRUNCATE TABLE civicrm_cache;
TRUNCATE TABLE civicrm_group_contact_cache;
EOF
}
function run_cleanup_job {
echo "[INFO] Running cleanup job"
"${PHP_CLI_CMD}" -f "${WP_DIR}/wp-content/plugins/civicrm/civicrm/bin/cli.php" -- -j -sdefault -u "${CIVICRM_USER}" -p "${CIVICRM_PASSWORD}" -e Job -a cleanup
}
function download_files {
echo "[INFO] Downloading civicrm files to ${civicrm_zip_path}"
curl -L -o "${civicrm_zip_path}" "https://sourceforge.net/projects/civicrm/files/civicrm-stable/${CIVICRM_VERSION}/civicrm-${CIVICRM_VERSION}-wordpress.zip"
echo "[INFO] Downloading language files to ${l10n_path}"
curl -L -o "${l10n_path}" "http://liquidtelecom.dl.sourceforge.net/project/civicrm/civicrm-stable/${CIVICRM_VERSION}/civicrm-${CIVICRM_VERSION}-l10n.tar.gz"
echo "[INFO] Downloading wp-cli.php"
curl -o "${wp_cli_path}" https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
}
function upgrade_db {
echo "[INFO] Updating db"
"${PHP_CLI_CMD}" "${wp_cli_path}" --- \
--path="${WP_DIR}" \
--user="${CIVICRM_USER}" \
--password="${CIVICRM_PASSWORD}" \
civicrm upgrade-db
}
function run_cron {
set -x
"${PHP_CLI_CMD}" -f "${WP_DIR}/wp-content/plugins/civicrm/civicrm/bin/cli.php" -- -j -sdefault -u "${CIVICRM_USER}" -p "${CIVICRM_PASSWORD}" -e Job -a execute
}
function error_handler {
if [[ "${ROLLBACK_ACTION}" == "ask" ]]; then
read -p "Looks like we run into issues updating civicrm, do you wish to rollback to the state prior to updating ($timestring)? [Y/N] " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
rollback
fi
elif [[ "${ROLLBACK_ACTION}" == "yes" ]]; then
rollback
fi
exit 1
}
function all {
download_files
backup_db
echo "[INFO] You can rollback to the state prior to the upate with '$0 restore $timestring'"
trap error_handler EXIT # Registrer the error handler as exit function
update_files
wipe_caches
run_cleanup_job
upgrade_db
trap '' EXIT # Do not trigger error handler when the update was successfull
}
function list_backups {
ls "${TEMP_DIR}/"civicrm-*.sql | grep -o -P '[0-9]+-[0-9]+-[0-9]+_[0-9]+-[0-9]+-[0-9]+'
}
mkdir -p "${TEMP_DIR}"
case "${1:-x}" in
list_backups) list_backups ;;
backup_db) backup_db ;;
download_files) download_files ;;
update_files) update_files ;;
wipe_caches) wipe_caches ;;
run_cleanup_job) run_cleanup_job ;;
upgrade_db) upgrade_db ;;
remove_temp_dir) remove_temp_dir ;;
run_cron) run_cron ;;
restore_db)
timestring="$2"
restore_db
;;
restore)
timestring="$2"
rollback
;;
ALL) all ;;
*) echo >&2 "usage: $0 ALL|list_backups|backup_db|download_files|update_files|wipe_caches|run_cleanup_job|upgrade_db|restore|restore_db|run_cron"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment