Name this script backup.cgi and put it in the same directory as your moin.cgi script.

To do: this script only backs up the text files of the wiki- it does not back up the user preferences or the diffs. It needs to be extended to do that! --AdamFeuer

#
# Simple script to back up the wiki
#
# Copyright 2001  Adam Feuer <adamf at pobox dot com>
#
# License: Python
#

import sys
import os
import stat
import string
import time
import socket

sys.path.append('.')
import moin_config 

true  = 1
false = 0

BUFSIZE = 100000  # how much of a file to read at once

wikiPath = moin_config.data_dir
wikiName = moin_config.wiki_name
wikiTextFolder = os.path.join(wikiPath,'text')

def MakeTimeString():
    return time.strftime("%Y-%m-%d-%H%M",time.localtime())

def MakeTempFileName(baseName):
    return "/tmp/%s_backup-%s.tar.gz" % (baseName,MakeTimeString())

def GzipDirectory(tempFileName,path):
    pid = os.fork()
    if pid == 0:
        # we are the child process
        parent, dir = os.path.split(path)
        os.chdir(parent)
        os.execl("/bin/tar","tar", "-zcf", tempFileName, dir)
        exit(0)
    else:
        os.waitpid(pid,0) # for child process to end

    size = os.stat(tempFileName)[stat.ST_SIZE]
    return size


def BackupWiki(wikiName,wikiTextFolder):
    tempFileName = MakeTempFileName(wikiName)
    return tempFileName, GzipDirectory(tempFileName,wikiTextFolder)


def DumpFile(path):
    f = open(path)
    buffer = f.read(BUFSIZE)
    while len(buffer) > 0:
        sys.stdout.write(buffer)
        buffer = f.read(BUFSIZE)
    f.close()

def GetHostname():
    return socket.getfqdn()

if __name__ == "__main__":

    print "Content-type: application/x-gzip"
    filename,size=BackupWiki(wikiName,wikiTextFolder)
    print "Content-length: %s" % size
    print "Content-Disposition: attachment; filename=%s" % filename
    print "Expires: 0"
    print

    DumpFile(filename)
    os.remove(filename)

SeaPigBackupSourceCode (last edited 2008-03-04 08:33:08 by localhost)