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:


date="$(date +%F-%T)"
db_user_name_and_db_name="SOME_IDENTICAL_NAMES" # DB user name and DB name must be the same;
domain="SOME_DOMAIN" # As site's directory;

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"


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}" \

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

1 Answer 1


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}"
    # handle error condition

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.

# Usage: how-to-use-script
# Purpose: upgrade all core MediaWiki websites
# etc
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 24, 2019 at 19:59
  • \$\begingroup\$ 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). \$\endgroup\$
    – user125391
    Commented Sep 5, 2019 at 11:08
  • \$\begingroup\$ @JohnDoea thanks, of course it's fine. \$\endgroup\$
    – suspectus
    Commented Sep 5, 2019 at 11:29
  • \$\begingroup\$ Hi, done, thanks so much; sorry for delaying and sorry for not accepting before - I should definitely have done that. \$\endgroup\$
    – user125391
    Commented Sep 11, 2019 at 3:05
  • \$\begingroup\$ @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 :-) \$\endgroup\$
    – Mast
    Commented Sep 11, 2019 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.