# All-core MediaWiki website upgrade script

I wrote the following Bash script to upgrade All-core MediaWiki websites (no added extensions/skins or images - besides logo image).

I already tested the essence of this script and it worked for me; MediaWiki was upgraded from 1.32.0. to 1.33.0 and I can use the site regularly; yet I'd be glad for a review:

#!/bin/sh

date="$(date +%F-%T)" db_user_name_and_db_name="SOME_IDENTICAL_NAMES" # DB user name and DB name must be the same; war="SOME_WEB_APPLICATION_ROOT" domain="SOME_DOMAIN" # As site's directory; target_url="LINK_TO_LATEST_MEDIA_WIKI_ARCHIVE_DOWNLOAD" mkdir -p "${war}/mediawiki_general_backups"
mkdir -p "${war}/mediawiki_specific_backups" zip -r "${war}/mediawiki_general_backups/${domain}-directory-backup-${date}.zip" "${war}/${domain}"
mysqldump -u "${db_user_name_and_db_name}" -p "${db_user_name_and_db_name}" > "${war}/mediawiki_general_backups/${domain}-database-backup-${date}.sql" files=( LocalSettings.php robots.txt .htaccess*${domain}.png
)

cd "${war}/${domain}"
cp "${files[@]}" "${war}/mediawiki_specific_backups"

rm -rf "${war}/${domain}"
mkdir "${war}/${domain}"
wget ${target_url} -O - | tar -xzv --strip-components 1 -C${war}/${domain} cp -a "${war}/mediawiki_specific_backups"/* "${war}/${domain}"
cd "${war}/${domain}"/maintenance
php update.php

### Sitemap creation ###

mkdir -p "${war}/${domain}/sitemap"

php maintenance/generateSitemap.php \
--memory-limit=50M \
--fspath=/"${war}/${domain}/sitemap" \
--identifier="${domain}" \ --urlpath=/sitemap/ \ --server=https://"${domain}" \
--compress=yes

# Sitemap should be declared in robots.txt / preferred search engine's search console, or both;


Naming Try not to use generic names like $file. Make your code read easy for others. Say $files_to_backup for example.

Check for errors when it matters If backup is important: check that the backups succeeded:

if cp "${files[@]}"${war}/mediawiki_specific_backups";then
rm -rf "${war}/${domain}"
mkdir "${war}/${domain}"
...
...
else
# handle error condition
fi


Document the code I prefer a comment in the script header describing what the script. If the script does useful work, document what it does so others understand what it does and how to use it. Similarly, comment anything in the code which is important and you think might require some explanation to help readers.

#!/bin/sh
# Usage: how-to-use-script
# Purpose: upgrade all core MediaWiki websites
# etc

• Code generally ends up looking cleaner if you handle the error case before the success case, because the error handler is usually shorter. Also, there are usually many paths to failure, and only one path to success, which should ideally appear at the end of the program. – 200_success Aug 24 at 19:59
• Hello, I up voted; is that okay for you that I'll add 2 more parts to the script (the original script won't change as it is always reviewed by one)? I want to add an mkdir command and a sitemap generation command (very common for many MediaWiki websites as it doesn't require extension and has a unique namespace). – JohnDoea Sep 5 at 11:08
• @JohnDoea thanks, of course it's fine. – suspectus Sep 5 at 11:29
• Hi, done, thanks so much; sorry for delaying and sorry for not accepting before - I should definitely have done that. – JohnDoea Sep 11 at 3:05
• @JohnDoea For the record, usually we're against updating the code in the question after answers have been received. It creates a mess if more answers come in, reviews of all different kinds of versions make it hard to follow what's going on. Please see what you may and may not do after receiving answers. However, since the author of the only answer so far is ok with it and the changes are minor, it's fine this time. Just, try not to do it again, ok? Thanks :-) – Mast Sep 11 at 7:31