MediaWiki backup via SSH sync

From FVue
Jump to: navigation, search


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.


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

Replace myuser, myhost and mydir with your own values.

#--- -----------------------------------------------
# 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/ 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/ phase2
    # Remove local incremental backup
rm incr.tgz

#--- ------------------------------------------------
# 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=./  # Checkpoint 1
time2=./  # Checkpoint 2
backup=incr.tgz         # Backup filename

case $1 in
            # 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 .

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



  • 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.


blog comments powered by Disqus