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