|
| 1 | +import logging |
| 2 | +import os |
| 3 | + |
| 4 | +from math import ceil |
| 5 | +from shutil import rmtree |
| 6 | +from time import time |
| 7 | + |
| 8 | +from mongodb_consistent_backup.Errors import OperationError |
| 9 | + |
| 10 | + |
| 11 | +class Rotate(object): |
| 12 | + def __init__(self, config, state_root, state_bkp): |
| 13 | + self.config = config |
| 14 | + self.state_root = state_root |
| 15 | + self.state_bkp = state_bkp |
| 16 | + self.backup_name = self.config.backup.name |
| 17 | + self.max_days = self.config.rotate.max_backup_days |
| 18 | + self.max_backups = self.config.rotate.max_backups |
| 19 | + |
| 20 | + self.latest = state_bkp.get("name") |
| 21 | + self.previous = None |
| 22 | + self.backups = self.backups_by_unixts() |
| 23 | + |
| 24 | + self.base_dir = os.path.join(self.config.backup.location, self.config.backup.name) |
| 25 | + self.latest_symlink = os.path.join(self.base_dir, "latest") |
| 26 | + self.previous_symlink = os.path.join(self.base_dir, "previous") |
| 27 | + |
| 28 | + self.max_secs = 0 |
| 29 | + if self.max_days > 0: |
| 30 | + seconds = float(self.max_days) * 86400.00 |
| 31 | + self.max_secs = int(ceil(seconds)) |
| 32 | + |
| 33 | + def backups_by_unixts(self): |
| 34 | + backups = {} |
| 35 | + for name in self.state_root.backups: |
| 36 | + backup = self.state_root.backups[name] |
| 37 | + backup_time = backup["updated_at"] |
| 38 | + backups[backup_time] = backup |
| 39 | + return backups |
| 40 | + |
| 41 | + def remove(self, ts): |
| 42 | + if ts in self.backups: |
| 43 | + backup = self.backups[ts] |
| 44 | + path = os.path.join(self.base_dir, backup["name"]) |
| 45 | + if os.path.isdir(path): |
| 46 | + logging.debug("Removing backup path: %s" % path) |
| 47 | + rmtree(path) |
| 48 | + else: |
| 49 | + raise OperationError("Backup path %s does not exist!" % path) |
| 50 | + if self.previous == backup["name"]: |
| 51 | + self.previous = None |
| 52 | + del self.backups[ts] |
| 53 | + |
| 54 | + def rotate(self): |
| 55 | + if self.max_days == 0 and self.max_backups == 0: |
| 56 | + logging.info("Backup rotation is disabled, skipping") |
| 57 | + return |
| 58 | + logging.info("Rotating backups (max_num=%i, max_days=%.2f)" % (self.max_backups, self.max_days)) |
| 59 | + kept_backups = 1 |
| 60 | + now = int(time()) |
| 61 | + for ts in sorted(self.backups.iterkeys(), reverse=True): |
| 62 | + backup = self.backups[ts] |
| 63 | + if not self.previous: |
| 64 | + self.previous = backup["name"] |
| 65 | + if self.max_backups == 0 or kept_backups < self.max_backups: |
| 66 | + if self.max_secs > 0 and (now - ts) > self.max_secs: |
| 67 | + logging.info("Backup %s exceeds max age %.2f days, removing backup" % (backup["name"], self.max_days)) |
| 68 | + self.remove(ts) |
| 69 | + continue |
| 70 | + logging.info("Keeping backup %s" % backup["name"]) |
| 71 | + kept_backups += 1 |
| 72 | + else: |
| 73 | + logging.info("Backup %s exceeds max backup count %i, removing backup" % (backup["name"], self.max_backups)) |
| 74 | + self.remove(ts) |
| 75 | + |
| 76 | + def symlink(self): |
| 77 | + try: |
| 78 | + if os.path.islink(self.latest_symlink): |
| 79 | + os.remove(self.latest_symlink) |
| 80 | + latest = os.path.join(self.base_dir, self.latest) |
| 81 | + logging.info("Updating %s latest symlink to current backup path: %s" % (self.backup_name, latest)) |
| 82 | + os.symlink(latest, self.latest_symlink) |
| 83 | + |
| 84 | + if os.path.islink(self.previous_symlink): |
| 85 | + os.remove(self.previous_symlink) |
| 86 | + if self.previous: |
| 87 | + previous = os.path.join(self.base_dir, self.previous) |
| 88 | + logging.info("Updating %s previous symlink to: %s" % (self.backup_name, previous)) |
| 89 | + os.symlink(previous, self.previous_symlink) |
| 90 | + except Exception, e: |
| 91 | + logging.error("Error creating backup symlinks: %s" % e) |
| 92 | + raise OperationError(e) |
0 commit comments