The following script upgrades an existing, all core (no add ons) MediaWiki install. I run it with set -x for debugging (I avoid set -euo because I often run this script line by line and/or block by block and in case of typos or problems this terminates the session, which I don't want to happen).
I have double tested the script and it is working fine (if your hosting provider requires you to add a directory such as public_html on top of web_application_root/domain than of course it should be added and the script should be updated accordingly).
By the way, I double create backups; before running this entire script I create filetree and DB backups from my hosting provider's tools as well and store them on my own computer device, just in case.
Code
File 1: General preface
#!/bin/bash
## Declare latest mediawiki core download link:
latest_mediawiki_core="https://releases.wikimedia.org/mediawiki/1.35/mediawiki-1.35.1.tar.gz"
## Declare web application variables:
read domain
read db_name
read db_nonroot_user_name
web_application_root="${HOME}/www"
domain_dir="${web_application_root}/${domain}"
File 2: Backups preface
#!/bin/bash
## Declare existing installation backup variables:
current_date="$(date +%d-%m-%Y-%H-%M-%S)" &&
general_backups_dir="${HOME}/mediawiki_general_backups" &&
specific_backups_dir="${HOME}/mediawiki_specific_backups"
## If fresh backup directories don't exist, create them:
# due to the rm -rf command below I personally prefer to first do a pre-script backup of both the DB and filetree
rm -rf "${general_backups_dir}" &&
mkdir -p "${general_backups_dir}" &&
rm -rf "${specific_backups_dir}" &&
mkdir -p "${specific_backups_dir}"
## Test existence of empty directories
ll "${general_backups_dir}" &&
ll "${specific_backups_dir}"
File 3: Database backups
#!/bin/bash
## Create database general backup:
mysqldump -u"$db_nonroot_user_name" -p "$db_name" > "${general_backups_dir}/${db_nonroot_user_name}-${current_date}.sql"
## Test database general backup:
ll $general_backups_dir
File 4: Filetree backups
#!/bin/bash
## Create filetree general backups:
zip -r "${general_backups_dir}/${domain}-directory-backup-${current_date}.zip" "${domain_dir}"
## Create filetree specific backups (Some files have shell glob patterns outside quote marks):
cp "${domain_dir}/LocalSettings.php" "${specific_backups_dir}" &&
cp "${domain_dir}/robots.txt" "${specific_backups_dir}" &&
cp "${domain_dir}/${domain}.png" "${specific_backups_dir}" &&
cp "${domain_dir}"/.htaccess* "${specific_backups_dir}" &&
cp "${domain_dir}"/google*.html "${specific_backups_dir}"
## Test filetree general backups:
ll $general_backups_dir
## Test filetree specific backups:
ll $specific_backups_dir
File 5: Installation
#!/bin/bash
## Prepare to download, install and configure MediaWiki and specific backups: ##
cd "${web_application_root}"
rm -rf "${domain_dir}"
## Download and configure MediaWiki core:
wget "${latest_mediawiki_core}" &&
find . -maxdepth 1 -iname 'mediawiki*.tar.gz' -type f -exec tar -xzf {} \; &&
find . -maxdepth 1 -type d -iname '*mediawiki*' -execdir mv {} "${domain}" \; &&
find . -maxdepth 1 -iname 'mediawiki*.tar.gz' -type f -exec rm {} \;
## Test previous operation: ##
ll
## Retrieve specific backups to the new installation:
cp -a "${specific_backups_dir}"/.htaccess "${domain_dir}" # Only .htaccess file;
cp -a "${specific_backups_dir}"/* "${domain_dir}" # All files besides .htaccess;
## Test specific backups retrieving:
ll ${domain_dir}
File 6: Configurations
## Prepare to create and configure a new sitemap and to update database:
cd "${domain_dir}"
## Create a new sitemap:
rm -rf "${domain_dir}/sitemap"
mkdir -p "${domain_dir}/sitemap"
php "${domain_dir}/maintenance/generateSitemap.php" \
--memory-limit=50M \
--fspath=/"${domain_dir}/sitemap" \
--identifier="${domain}" \
--urlpath=/sitemap/ \
--server=https://"${domain}" \
--compress=yes
# Ensure that robots.txt sitemap directive is:
# Sitemap: https://"${domain}"/sitemap/sitemap-index-"${domain}".xml
## Update database (One might need to change LocalSettings.php before doing so):
php "${domain_dir}/maintenance/update.php" --quick &&
php maintenance/rebuildrecentchanges.php