#!/usr/bin/python # -*- coding: utf-8 -*- import datetime import getopt import os import sys import tarfile def help(): help_msg = '''Usage: backup.py -r dokuwiki_root [-a] [-p] (filename_prefix) -r: dokuwiki root path -a: backup all dokuwiki data -p: backup with user plugins -h: print this message * Backup target: conf, data/{attic, media, media_attic, meta, pages} * Output file name: (filename_prefix)_YYYYMMDD(state).tar.bz2 e.g.) backup_20130719.tar.bz2 # only backup target backup_20130719a.tar.bz2 # all snapshot backup_20130719p.tar.bz2 # backup target with user-installed plugins''' print help_msg def main(argv): optlist, args = getopt.getopt(argv[1:], 'r:aph') # check optlist dokuwiki_root = None backup_all = False with_plugin = False for opt in optlist: if opt[0] == '-r': dokuwiki_root = opt[1] if opt[0] == '-a': backup_all = True if opt[0] == '-p': with_plugin = True if opt[0] == '-h': help() return 0 # check args if len(args) != 1: print >> sys.stderr, 'Error in filename_prefix' return 1 file_prefix = args[0] if dokuwiki_root is None: print >> sys.stderr, 'dokuwiki_root not given' return 1 backup_dokuwiki(dokuwiki_root, backup_all, with_plugin, file_prefix) def backup_dokuwiki(dokuwiki_root, backup_all, with_plugin, file_prefix): # minimum list of backup # https://www.dokuwiki.org/faq:backup DIRS_TO_BACKUP = ('conf', 'data/attic', 'data/media', 'data/media_attic', 'data/meta', 'data/pages') DEFAULT_PLUGIN = set(['acl', 'authad', 'authldap', 'authmysql', 'authpgsql', 'authplain', 'config', 'info', 'plugin', 'popularity', 'revert', 'safefnrecode', 'testing' , 'usermanager']) PLUGIN_PATH_PREFIX = 'lib/plugins' statecode = '' if backup_all: statecode = 'a' elif with_plugin: statecode = 'p' datetimestr = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d') filename = '%s_%s%s.tar.bz2' % (file_prefix, datetimestr, statecode) oldpath = os.getcwd() os.chdir(dokuwiki_root) tar = tarfile.open(filename, 'w:bz2') if backup_all: tar.add('./') else: for dir in DIRS_TO_BACKUP: tar.add('./' + dir) # plugin if with_plugin: for dir in os.listdir(PLUGIN_PATH_PREFIX): path = os.path.join(PLUGIN_PATH_PREFIX, dir) isdefault = dir in DEFAULT_PLUGIN # only archive user-install plugins if os.path.isdir(path) and not isdefault: tar.add('./'+path) os.chdir(oldpath) tar.close() print filename return 0 if __name__ == '__main__': sys.exit(main(sys.argv))