1 #!/usr/bin/env python 2 3 import sys, os, commands 4 5 try: 6 wiki_dir, archive_dir = sys.argv[1:3] 7 except ValueError: 8 print "For archiving, please specify the Wiki directory and a target archive directory." 9 print "For restoring, please specify the archive directory and a target Wiki directory." 10 sys.exit(1) 11 12 dry_run = ("-n" in sys.argv) 13 14 pages = os.path.join(wiki_dir, "wiki", "data", "pages") 15 users = os.path.join(wiki_dir, "wiki", "data", "user") 16 17 # Make the archive's page directory. 18 19 archive_pages = os.path.join(archive_dir, "wiki", "data", "pages") 20 if not dry_run and not os.path.exists(archive_pages): 21 os.makedirs(archive_pages) 22 23 archive_users = os.path.join(archive_dir, "wiki", "data", "user") 24 if not dry_run and not os.path.exists(archive_users): 25 os.makedirs(archive_users) 26 27 # Copy user details. 28 29 print "Copying users..." 30 if not dry_run: 31 cmd = "cp %s %s" % (os.path.join(users, "*"), archive_users) 32 print cmd 33 os.system(cmd) 34 35 # Find active Wiki pages. 36 37 for page in os.listdir(pages): 38 page_dir = os.path.join(pages, page) 39 40 if os.path.isdir(page_dir) and "current" in os.listdir(page_dir): 41 42 f = open(os.path.join(page_dir, "current")) 43 try: 44 current = f.read().strip() 45 finally: 46 f.close() 47 48 archive_page_dir = os.path.join(archive_pages, page) 49 archive_page_revisions = os.path.join(archive_page_dir, "revisions") 50 page_revisions = os.path.join(page_dir, "revisions") 51 52 if current not in os.listdir(page_revisions): 53 continue 54 55 print "Archiving", page 56 print "Revision", current 57 58 if not dry_run: 59 if not os.path.exists(archive_page_revisions): 60 os.makedirs(archive_page_revisions) 61 62 f = open(os.path.join(archive_page_dir, "current"), "w") 63 try: 64 f.write("00000001") 65 finally: 66 f.close() 67 68 cmd = "cp %s %s" % tuple( 69 map(commands.mkarg, (os.path.join(page_revisions, current), os.path.join(archive_page_revisions, "00000001"))) 70 ) 71 print cmd 72 if not dry_run: 73 os.system(cmd) 74 75 attachments_dir = os.path.join(page_dir, "attachments") 76 if os.path.exists(attachments_dir): 77 print "Archiving attachments..." 78 79 archive_attachments_dir = os.path.join(archive_page_dir, "attachments") 80 if not dry_run and not os.path.exists(archive_attachments_dir): 81 os.mkdir(archive_attachments_dir) 82 83 cmd = "cp %s %s" % ( 84 os.path.join(commands.mkarg(attachments_dir), "*"), commands.mkarg(archive_attachments_dir) 85 ) 86 print cmd 87 if not dry_run: 88 os.system(cmd) 89 90 print 91 92 print "Now run the following, if adding to a live Wiki:" 93 print "chown -R www-data: %s" % os.path.join(archive_dir, "wiki") 94 95 # vim: tabstop=4 expandtab shiftwidth=4