MediaWiki backup via SSH sync

From FVue
Jump to: navigation, search

Problem

I want to backup MediaWiki files, as AdminSettings.php, LocalSettings.php, images/* and .htaccess, but my Internet Service Provicer doesn't allow me to run rsync.

NOTE: Besides the files you also want to backup the MediaWiki database. See: MediaWiki database backup via SSH tunnel for a short bash script which copies a remote MediaWiki database locally.

Solution

Create a directory sshsync both remote and local. Place sync_master.sh in the local directory and place _sync_slave.sh in the remote directory.

Replace myuser, myhost and mydir with your own values.

sync_master.sh

#!/bin/bash
#--- sync_master.sh -----------------------------------------------
# Backup incremental

set -o errexit  # Exit on error
set -o nounset  # Trigger error when expanding unset variables
cd "$(dirname "$(which "$0")")"  # Cd to script dir

    # Perform incremental back-up on server
ssh myuser@myhost sshsync/_sync_slave.sh phase1
    # Move incremental backup to here
scp myuser@myhost:sshsync/incr.tgz .
    # Untar incremental backup
tar -C.. -xvzf incr.tgz
    # Reset incremental back-up on sever
ssh myuser@myhost sshsync/_sync_slave.sh phase2
    # Remove local incremental backup
rm incr.tgz

_sync_slave.sh

#!/bin/bash
#--- _sync_slave.sh ------------------------------------------------
# Backup incremental
# @param string $1  Phase.  Either `phase1' or `phase2'.

set -o errexit  # Exit on error
set -o nounset  # Trigger error when expanding unset variables
    # Cd to script dir
cd "$(dirname "$(which "$0")")"

    # Check arguments
[[ $# == 0 || ! ($1 == 'phase1' || $1 == 'phase2') ]] && {
    echo "Usage: $0 phase1|phase2"
    exit 2
}

    # Initialize variables

time1=./afterdate1.be~  # Checkpoint 1
time2=./afterdate2.be~  # Checkpoint 2
backup=incr.tgz         # Backup filename

case $1 in
    phase1)
            # If backup file exist, exit with error
        [ -f $backup ] && { echo "Error: backup file exists \`$PWD/$backup'"; exit 1; }
            # Make sure checkpoint 1 exists
        [ -f $time1 ] || touch -t 197001010100 $time1
            # Create checkpoint 2 as of now
        touch $time2
            # Create tar of those files, whose status changed after checkpoint 1
        tar --ignore-failed-read --newer=$time1 -C $HOME/mydir -cvzf $backup .
        ;;

    phase2)
            # Remove backup
        rm $backup
            # Move checkpoint 2 to checkpoint 1 for next run
        mv $time2 $time1
        ;;
esac

Journal

20070816

  • Added tar option `--ignore-failed-read' to prevent tar from exitting with error when file changes while creating tar.
  • Changed `--newer-mtime' to `--newer' in order to also synchronize files whose content didn't change but did have a status change, e.g. file renamed.

Comments

blog comments powered by Disqus