Skip to content

Commit 5414a17

Browse files
Basic rotation logic working
1 parent be3ce3f commit 5414a17

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

mongodb_consistent_backup/Common/Config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def makeParser(self):
7373
parser.add_argument("--ssl.client_cert_file", dest="ssl.client_cert_file", help="Path to Client SSL Certificate file in PEM format (for optional client ssl auth)", default=None, type=str)
7474
parser.add_argument("-L", "--log-dir", dest="log_dir", help="Path to write log files to (default: disabled)", default='', type=str)
7575
parser.add_argument("--lock-file", dest="lock_file", help="Location of lock file (default: /tmp/mongodb-consistent-backup.lock)", default='/tmp/mongodb-consistent-backup.lock', type=str)
76+
parser.add_argument("--rotate.max_backups", dest="rotate.max_backups", help="Maximum number of backups to keep in backup directory (default: disabled)", default=0, type=int)
77+
parser.add_argument("--rotate.max_backup_days", dest="rotate.max_backup_days", help="Maximum age in days for backups in backup directory (default: disabled)", default=0, type=int)
7678
parser.add_argument("--sharding.balancer.wait_secs", dest="sharding.balancer.wait_secs", help="Maximum time to wait for balancer to stop, in seconds (default: 300)", default=300, type=int)
7779
parser.add_argument("--sharding.balancer.ping_secs", dest="sharding.balancer.ping_secs", help="Interval to check balancer state, in seconds (default: 3)", default=3, type=int)
7880
return self.makeParserLoadSubmodules(parser)

mongodb_consistent_backup/Main.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from Notify import Notify
1515
from Oplog import Tailer, Resolver
1616
from Replication import Replset, ReplsetSharded
17+
from Rotate import Rotate
1718
from Sharding import Sharding
18-
from State import StateRoot, StateBackup, StateBackupReplset, StateDoneStamp, StateOplog
19+
from State import StateRoot, StateBackup, StateBackupReplset, StateOplog
1920
from Upload import Upload
2021

2122

@@ -97,8 +98,9 @@ def set_backup_dirs(self):
9798
self.backup_directory = os.path.join(self.config.backup.location, self.backup_root_subdirectory)
9899

99100
def setup_state(self):
100-
StateRoot(self.backup_root_directory, self.config).write(True)
101-
self.state = StateBackup(self.backup_directory, self.config, self.backup_time, self.uri, sys.argv)
101+
self.state_root = StateRoot(self.backup_root_directory, self.config)
102+
self.state = StateBackup(self.backup_directory, self.config, self.backup_time, self.uri, sys.argv)
103+
self.state_root.write(True)
102104
self.state.write()
103105

104106
def setup_notifier(self):
@@ -167,6 +169,10 @@ def update_symlinks(self):
167169
logging.info("Updating %s latest symlink to: %s" % (self.config.backup.name, self.backup_directory))
168170
return os.symlink(self.backup_directory, self.backup_latest_symlink)
169171

172+
def rotate_backups(self):
173+
rotater = Rotate(self.config, self.state_root)
174+
rotater.run()
175+
170176
# TODO Rename class to be more exact as this assumes something went wrong
171177
# noinspection PyUnusedLocal
172178
def cleanup_and_exit(self, code, frame):
@@ -451,6 +457,7 @@ def run(self):
451457

452458
# stop timer
453459
self.stop_timer()
460+
self.state.set("completed", True)
454461

455462
# send notifications of backup state
456463
try:
@@ -466,8 +473,8 @@ def run(self):
466473
self.notify.close()
467474
self.exception("Problem running Notifier! Error: %s" % e, e)
468475

469-
StateDoneStamp(self.backup_directory, self.config).write()
470476
self.update_symlinks()
477+
self.rotate_backups()
471478

472479
self.logger.rotate()
473480
logging.info("Completed %s in %.2f sec" % (self.program_name, self.timer.duration(self.timer_name)))

mongodb_consistent_backup/State.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ def merge(self, new, old):
3737
merged.update(new)
3838
return merged
3939

40-
def load(self, load_one=False):
40+
def load(self, load_one=False, filename=None):
4141
f = None
42+
if not filename:
43+
filename = self.state_file
4244
try:
43-
f = open(self.state_file, "r")
45+
f = open(filename, "r")
4446
data = decode_all(f.read())
4547
if load_one and len(data) > 0:
4648
return data[0]
@@ -94,6 +96,7 @@ def __init__(self, base_dir, config, backup_time, seed_uri, argv=None):
9496
StateBase.__init__(self, base_dir, config)
9597
self.base_dir = base_dir
9698
self.state['backup'] = True
99+
self.state['completed'] = False
97100
self.state['name'] = backup_time
98101
self.state['method'] = config.backup.method
99102
self.state['path'] = base_dir
@@ -128,6 +131,8 @@ def __init__(self, base_dir, config):
128131
StateBase.__init__(self, base_dir, config)
129132
self.base_dir = base_dir
130133
self.state['root'] = True
134+
self.backups = {}
135+
self.completed_backups = 0
131136

132137
self.init()
133138

@@ -136,7 +141,6 @@ def init(self):
136141
self.load_backups()
137142

138143
def load_backups(self):
139-
backups = []
140144
if os.path.isdir(self.base_dir):
141145
for subdir in os.listdir(self.base_dir):
142146
try:
@@ -145,16 +149,10 @@ def load_backups(self):
145149
continue
146150
state_path = os.path.join(bkp_path, self.meta_name)
147151
state_file = os.path.join(state_path, "meta.bson")
148-
done_path = os.path.join(state_path, "done.bson")
149-
if os.path.isdir(state_path) and os.path.isfile(state_file) and os.path.isfile(done_path):
150-
backups.append(state_file)
152+
self.backups[subdir] = self.load(True, state_file)
153+
if self.backups[subdir]["completed"]:
154+
self.completed_backups += 1
151155
except:
152156
continue
153-
logging.info("Found %i existing completed backups for set" % len(backups))
154-
return backups
155-
156-
157-
class StateDoneStamp(StateBase):
158-
def __init__(self, base_dir, config):
159-
StateBase.__init__(self, base_dir, config, "done.bson")
160-
self.state = {'done': True}
157+
logging.info("Found %i existing completed backups for set" % self.completed_backups)
158+
return self.backups

0 commit comments

Comments
 (0)