-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongobackup.py
executable file
·77 lines (58 loc) · 2.04 KB
/
mongobackup.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import argparse
import datetime
import subprocess
__author__ = 'Robin Tiwari'
__version__ = '0.1.0'
__description__ = 'MongoDB backup and restore utility'
__license__ = 'MIT'
parser = argparse.ArgumentParser(description='backup/restore mongodb')
parser.add_argument('-d', '--database',
type=str,
required=False,
default=None,
help='database')
parser.add_argument('-o', '--output',
type=str,
required=False,
default=None,
help='output directory')
parser.add_argument('-u', '--user',
type=str,
required=False,
default=None,
help='mongodb user')
parser.add_argument('-p', '--password',
type=str,
required=False,
default=None,
help='mongodb password')
parser.add_argument('--port',
type=str,
required=False,
default=None,
help='mongodb port')
def backup(args):
backup_path = os.path.join(os.path.expanduser("~"), 'backup')
if args.output is not None:
backup_path = args.output
now = datetime.datetime.today().strftime('%Y%m%d-%H%M%S')
backup_name = os.path.join(backup_path, now)
os.mkdir(backup_name)
if args.output is not None:
backup_path = args
stat = subprocess.check_output('mongodump',
'-host', 'localhost',
'-u', '%s' % args.user,
'-p', '%s' % args.password,
'-d', '%s' % args.database,
'--port', '%s' % args.port,
'-o', '%s' % backup_path)
print(stat)
def restore():
pass
if __name__ == '__main__':
args = parser.parse_args()
backup(args)