Automatically Backup Dokuwiki Data Using Shell Script

Dokuwiki is an elegant and concise wiki-site. Since it uses plain text files instead of a database to store pages, one offline copy could be useful for some situations, like read it on your Kindle when you go offline or publish your ebook. Thus, we want to back up our pages and media file daily and create a symbolic link to the latest copy for downloads.

The official Dokuwiki provides varies of backup scripts. Unfortunately, the bash script is no longer working since Dokuwiki changed project structure, so I modified this script and added some feature. This script is tested under bash and I think it probably works in other shell environments as well.

Shell file:

# Automatically Backup Dokuwiki Data Using Shell Script
# $Id: 328 2004-12-22 13:15:20Z dp $
# Original script:
# Blog post:

# config 
WIKIPATH="/var/www/" # path to your wiki data directory, no symbolic links are allowed!
# for debian etch: /var/lib/dokuwiki
BACKUPPATH="/var/www/" # where do you save the backups?
DAILY_DATA_BACKUPS="8" # keep this amount data backups
DAILY_MEDIA_BACKUPS="3" # and media backups

FILE_PREFIX="lxfy_wiki" # filename for the latest backup to download
# no more config
# creates $1, if not existant
	if [ ! -d "${BACKUPPATH}/$1" ]
		mkdir -p "${BACKUPPATH}/$1"
# 1 -> path
# 2 -> name
# 3 -> number of backups
	for i in `seq $(($3 - 1)) -1 1`
		if [ -f "$1/$2.$i.tar.gz" ]
			mv "$1/$2.$i.tar.gz" "$1/$2.$((i + 1)).tar.gz"
# make sure everything exists
checkDir "data"
checkDir "data/archive"
checkDir "data/daily"
checkDir "media"
checkDir "media/archive"
checkDir "media/daily"
# first step: rotate daily.
rotateDir "${BACKUPPATH}/data/daily" "data" "$DAILY_DATA_BACKUPS"
rotateDir "${BACKUPPATH}/media/daily" "media" "$DAILY_MEDIA_BACKUPS"
# then create our backup
#   --exclude is not accepted for Linksys NSLU2 box, any alternative?
tar --exclude=".*" -zcf "/tmp/data.1.tar.gz" -C "${WIKIPATH}" "pages"
tar --exclude=".*" -zcf "/tmp/media.1.tar.gz" -C "${WIKIPATH}" "media"
# for debian etch, replace "media" by "data/media" in line above
# and add --exclude="media" to first tar line
# create an archive backup?
if [ `date +%d` = "01" ]
	cp "/tmp/data.1.tar.gz" "${BACKUPPATH}/data/archive/data-"`date +%m-%d-%Y`".tar.gz"
	cp "/tmp/media.1.tar.gz" "${BACKUPPATH}/media/archive/media-"`date +%m-%d-%Y`".tar.gz"
# add them to daily.
mv "/tmp/data.1.tar.gz" "${BACKUPPATH}/data/daily"
mv "/tmp/media.1.tar.gz" "${BACKUPPATH}/media/daily"

# add symbolic links for download

ln -sf "${BACKUPPATH}/data/daily/data.1.tar.gz" "${BACKUPPATH}/${FILE_PREFIX}_data.tar.gz"
ln -sf "${BACKUPPATH}/media/daily/media.1.tar.gz" "${BACKUPPATH}/${FILE_PREFIX}_media.tar.gz"

In this scenario, we compress the Dokuwiki pages and media folder which contains the text content and accessories like pictures. These two folders locate under the your_doku_path/data directory (NOT Doku directory), so change your WIKIPATH constant to your_doku_path/data, like /var/www/ All the backed up files will be stored in BACKUPPATH, furthermore, two symbolic links will be generated to the backup path for downloads, so if you want others to download your wiki, make sure the backup folder is visible for visitors and spread these two URL, otherwise comment the last two lines of script and set folder permission invisible for visitors. To periodically backup and update files, you may need to set up a cron job, simply add your time-frequency and command /usr/bin/bash /your_script_path/ to crontab and that’s it.

Wist this script helps and if you found any issue, please contact me or leave a comment below.

Ref: How to backup DokuWiki?


您的电子邮箱地址不会被公开。 必填项已用*标注