Skip to content

Instantly share code, notes, and snippets.

@luoxufeiyan
Created December 18, 2019 08:25
Show Gist options
  • Save luoxufeiyan/8d4ce8ffd2a7a8d83e63ea47c8f6e3a2 to your computer and use it in GitHub Desktop.
Save luoxufeiyan/8d4ce8ffd2a7a8d83e63ea47c8f6e3a2 to your computer and use it in GitHub Desktop.
Automatically Backup Dokuwiki Data Using Shell Script
#!/bin/sh
# Automatically Backup Dokuwiki Data Using Shell Script
# $Id: dw-backup.sh 328 2004-12-22 13:15:20Z dp $
# Original script: https://www.dokuwiki.org/tips:backup_script
# Blog post: https://www.luoxufeiyan.com/?p=3949
# config
WIKIPATH="/var/www/wiki.luoxufeiyan.com/data" # path to your wiki data directory, no symbolic links are allowed!
# for debian etch: /var/lib/dokuwiki
BACKUPPATH="/var/www/wiki.luoxufeiyan.com/backup" # 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
checkDir()
{
if [ ! -d "${BACKUPPATH}/$1" ]
then
mkdir -p "${BACKUPPATH}/$1"
fi
}
# 1 -> path
# 2 -> name
# 3 -> number of backups
rotateDir()
{
for i in `seq $(($3 - 1)) -1 1`
do
if [ -f "$1/$2.$i.tar.gz" ]
then
mv "$1/$2.$i.tar.gz" "$1/$2.$((i + 1)).tar.gz"
fi
done
}
# 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" ]
then
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"
fi
# 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment