Back up your MediaWiki

From XPUB & Lens-Based wiki

Make a "backup" folder in your homefolder on the server

$ mkdir ~/backup

Collect the different things to backup

database

make a copy of the database in MySQL

save it to a file, for example: "backup.sql"

to "dump" a database, you can run:

$ mysql -u DBUSERNAME -p DBPASS DATABASE_NAME * ~/backup/backup.sql
$ mysql -u tisaneza -p DBPASS tisabase * ~/backup/backup.sql

(You can find these details in your LocalSettings.php, for Tisa it looks like this:)

$wgDBtype = "mysql";
$wgDBserver = "localhost";
$wgDBname = "tisabase";
$wgDBuser = "tisaneza";

media files

these are saved to: /path/to/wiki/images/

copy this folder, and also copy it to another server

$ cp -r /path/to/wiki/images ~/backup/
$ cp -r /var/www/html/tisawiki/images ~/backup/

localsettings.php

this file is saved in: /path/to/wiki/

$ cp /path/to/wiki/LocalSettings.php ~/backup/
$ cp /var/www/html/tisawiki/LocalSettings.php ~/backup/

extensions folder

these are saved in: /path/to/wiki/extensions/

$ cp -r /path/to/wiki/extensions ~/backup/
$ cp -r /var/www/html/tisawiki/extensions ~/backup/

Zip it !

We will add the date of today to the filename of the zip file.

$ sudo apt-get install zip
$ zip -r "backup-$(date +"%Y-%m-%d").zip" backup.sql LocalSettings.php images extensions

To see what "$(date +"%Y-%m-%d")" outputs:

$ echo $(date +"%Y-%m-%d")

Copy it to another server OR usb stick

another server

This step requires: ssh access from your server, to another machine that is available on the network

make a ssh key for your server

add your public key to the other machine

try if the ssh connection works!!

$ ssh user@othermachine
$ ssh user@ipadress

Then send the backup to the other server with:

$ scp backup.zip anotherserver:/path/to/backup

usb stick

connect the usb to the Pi

access the mount point of the USB

Check /media/ to see if your USB is there.

$ cd /media/
$ ls -la

If this is not the case, we will mount your USB first.

Find the device path of your USB:

$ lsblk

Often the path is something like: /dev/sda

And the partition ("part") is: /dev/sda1

Now mount your USB stick using this device path.

First make the folder that you will mount the USB to:

$ sudo mkdir /mnt/usb/

And then mount it, using the partition path:

$ sudo mount /dev/sda1 /mnt/usb/

To unmount the USB (to check if your backups work):

$ sudo umount /media/usb

or

$ sudo umount /dev/sda

Now let's copy the ZIP file to the USB stick

If you use /mnt/usb/:

$ sudo cp ~/backup/backup.zip /mnt/usb/

If you use /media/usb/:

$ sudo cp ~/backup/backup.zip /media/usb/

Automate this!

Make a bash script:

$ nano backup.sh

Copy all the commands we ran and copy paste them into your bash script.

It would look like this:

$ mysql -u USERNAME -p DBPASS PASSWORD * ~/backup/backup.sql
$ cp -r /var/www/html/WIKIFOLDER/images ~/backup/
$ cp /var/www/html/WIKIFOLDER/LocalSettings.php ~/backup/
$ cp -r /var/www/html/WIKIFOLDER/extensions ~/backup/
$ zip -r ~/backup/backup.zip ~/backup/backup.sql ~/backup/LocalSettings.php ~/backup/images ~/backup/extensions
$ sudo cp ~/backup/backup.zip /mnt/usb/

Let's try to run it.

$ bash backup.sh

And now let's run this script every night!

We do this with cron.

Every user on the system has its own so called "crontab".

Let's use the one from the root user, we edit it with -e:

$ sudo crontab -e

On a new line, add the following command, it will execute our backup.sh script every night at 5am:

0 5 * * * ~/bash backup.sh

TIP: https://crontab.guru/ (useful to write the time code)