3
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This question was linked to at the MediaWiki support desk \$\endgroup\$
    – pppery
    Feb 20 at 17:38
  • \$\begingroup\$ Yes, and BTW, is there someone here whom will agree to add a "MediaWiki" tag in the tags section? Some MediaWiki engineers are registered users in the wide community and might get an RSS feed about this thread so (I think). \$\endgroup\$
    – timesharer
    Feb 20 at 17:39
0
\$\begingroup\$

Some remarks

File 1: General preface

## Declare web application variables:
read domain
read db_name
read db_nonroot_user_name

When the user is prompted for a string, please tell what is wanted:

read -p "Domain: " domain
read -p "Database name: " db_name
read -p "Datebase user (not admin): " db_nonroot_user_name

File 2: Backups preface

## 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"

Remove the &&, the assignment will work.

rm -rf "${general_backups_dir}" &&
mkdir -p "${general_backups_dir}" &&
rm -rf "${specific_backups_dir}" &&
mkdir -p "${specific_backups_dir}"

You do not have a check on the last command. When another command fails, the next command is skipped, but the script continues. Consider error handling like

die() {
   echo "Fatal error: $*"
   exit 1
}

rm -rf "${general_backups_dir}" || die "Removing ${general_backups_dir} failed."
mkdir -p "${general_backups_dir}" || die "Creating ${general_backups_dir} failed."
rm -rf "${specific_backups_dir}" || die "Removing ${specific_backups_dir} failed."
mkdir -p "${specific_backups_dir}" || die "Creating ${specific_backups_dir} failed.

"

## Test existence of empty directories
ll "${general_backups_dir}" &&
ll "${specific_backups_dir}"

Test is not needed with the die construction. When you want to test, use

test -d "${general_backups_dir}" || die "Missing ${general_backups_dir}." 

File 4: Filetree backups

## Create filetree specific backups 
cp "${domain_dir}/LocalSettings.php" "${specific_backups_dir}" && ...

Test the copy result with

cp "${domain_dir}/LocalSettings.php" "${specific_backups_dir}" || die "cp LocalSettings failed."

File 5: Installation

## Prepare to download, install and configure MediaWiki and specific backups: ##
cd "${web_application_root}"

The cd commands are really important. What will happen, when the user doesn't has a www directory?

cd "${web_application_root}" || die "cd ${web_application_root} failed"

Remove with care

rm -rf "${domain_dir}"

Sure about this? Perhaps the user hit enter by accident when he was asked for a domain.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.