-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchdb-tools.py
executable file
·104 lines (86 loc) · 3.4 KB
/
couchdb-tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import argparse
import json
import os
import hashlib
import shutil
import glob
import time
import gc
import concurrent.futures
import couchdb
from furl import furl
import humanize
import re
### Functions
def get_json_documents(lines):
documents = []
for line in lines:
documents.append(json.loads(line.strip()))
return documents
### get_json_documents
def process_database(database):
if args.match and not re_match.match(database):
return "No match for DB " + database + ""
if args.exclude and re_exclude.match(database):
return "Excluding match for DB " + database + ""
buffer = []
initial_size = humanize.naturalsize(client[database].info()['data_size'])
if args.delete:
try:
del client[database]
buffer.append("Deleted: " + database)
except:
buffer.append("Failed to delete: " + database)
if args.rebuild:
try:
del client[database]
client.create(database)
except:
buffer.append("Failed to delete: " + database)
if args.compact:
try:
client[database].cleanup()
client[database].compact()
except:
buffer.append("Failed to compact: " + database)
try:
buffer.append (database + ' ::: ' + initial_size + ' -> ' + humanize.naturalsize(client[database].info()['data_size']) + ' ::: ' + client[database].resource.url)
except Exception:
pass
print ("\n".join(buffer))
### process_database
### Functions
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Dumps CouchDB / Bigcouch databases')
parser.add_argument('--host', help='FQDN or IP, including port. Default: http://localhost:5984', default='http://localhost:5984')
parser.add_argument('--user', help='DB username. Default: none')
parser.add_argument('--password', help='DB password. Default: none')
parser.add_argument('--delete', help='Delete matching DBs, and not recreate them.', action="store_true")
parser.add_argument('--rebuild', help='Delete DB and recreate it again', action="store_true")
parser.add_argument('--clean', help='Delete all docs in matching DBs, preserves views', action="store_true")
parser.add_argument('--purge', help='Purge all docs in matching DBs', action="store_true")
parser.add_argument('--compact', help='Cleanup and Compact all docs in matching DBs', action="store_true")
parser.add_argument('--match', help='Regular expression to match the DB names. Example ".*-myprogram|users|.*bkp.*". Default: None.')
parser.add_argument('--exclude', help='Regular expression to match the DB names for exclusion. Example ".*-myprogram|users|.*bkp.*". Default: None.')
args = parser.parse_args()
print(args)
url = furl(args.host)
url.username = args.user
url.password = args.password
client = couchdb.Server(str(url))
# Filter databases
if args.match:
re_match = re.compile(args.match)
print ('Regular expresion will be used to filter databases')
if args.exclude:
re_exclude = re.compile(args.exclude)
print ('Regular expresion will be used to filter databases for exclusion')
databases = list(client)
for database in databases:
try:
process_database(database)
except Exception as exc:
print (exc)