Skip to content

Instantly share code, notes, and snippets.

@svenk
Created September 16, 2021 10:38
Show Gist options
  • Save svenk/57c3930825b5e9e403a26ab6922d2ac8 to your computer and use it in GitHub Desktop.
Save svenk/57c3930825b5e9e403a26ab6922d2ac8 to your computer and use it in GitHub Desktop.
Mediawiki backup to Git Repository

Automatic backup to some SECURE git space for a MediaWiki

This is carried out with two shell scripts: One responsible for gathering the wiki contents in various formats as single files, and one responsible for uploading to a git repository.

Directory structures

The directory structure of the wiki installation is

w
├── api.php
├── autoload.php
├── backupper
├── ...
├── LocalSettings.php
├── ...
└── vendor

And the one of the backupper is like

backupper
├── assets
│   ├── database.sql
│   ├── full-dump.xml
│   └── wikidata.tar.gz
├── assets-git
│   ├── database.sql
│   ├── full-dump.xml
│   ├── README.md
│   └── wikidata.tar.gz
├── backup-private.sh
├── git-backup.sh
└── README.txt

The directory assets is filled by backup-private.sh and assets-git is the work place for git-backup.sh. Both are rather temporary, as backups shall leave the server fast.

Single usage

Adopt the scripts, execute them (first the backup, then git upload).

Regular usage

Usage is just execute both scripts in a cron line like:

0 5 * * * /path/to/your/installation/w/backupper/cron.sh

e.g. run crontab -e from the relevant user, install that line, done.

#!/bin/bash
# This backups the mediawiki installation.
set -e
mkdir -p assets
pass=$(grep wgDBpassword ../LocalSettings.php | awk -F'"' '{print $2}')
user=$(grep wgDBuser ../LocalSettings.php | awk -F'"' '{print $2}')
name=$(grep wgDBname ../LocalSettings.php | awk -F'"' '{print $2}')
host=$(grep wgDBserver ../LocalSettings.php | awk -F'"' '{print $2}')
MYSQL_PWD=$pass mysqldump --no-tablespaces -h $host -u $user $name > assets/database.sql
php ../maintenance/dumpBackup.php --full > assets/full-dump.xml
tar zcvfh assets/wikidata.tar.gz --exclude="backupper" ..
# Also copy the docs
cp *.txt *.sh assets/
#!/bin/bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd $SCRIPT_DIR
nice ./backup-private.sh && ./git-backup.sh
#!/bin/bash
#
# This script copies the contents of a directory to the git master
# of a given repository. It thus can abuse Git for revisioned
# backups. Sweet!
# 2021, Public Domain, SvenK
backup_directory="assets"
git_work_directory="assets-git"
# these are secrets:
user="<YOUR USERNEME. better make a dedicated token at github/gitlab/etc>"
pass="<YOUR PASS>"
remote="https://$user:$pass@githost/the/path.git"
set -e
rm -rf $git_work_directory
git clone --depth=1 $remote $git_work_directory
cp $backup_directory/* $git_work_directory
cd $git_work_directory
git add .
git commit -m"New backup carried out on $(hostname) by $(whoami) at local $(date)."
git push
cd ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment