Skip to content

Commit

Permalink
Add --keep-days to clean-backups command. Fixes #216.
Browse files Browse the repository at this point in the history
  • Loading branch information
rbvermaa committed Dec 13, 2016
1 parent f60ee22 commit 2bb6205
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 15 additions & 6 deletions nixops/deployment.py
Expand Up @@ -18,7 +18,7 @@
import nixops.parallel
from nixops.nix_expr import RawValue, Function, Call, nixmerge, py2nix
import re
from datetime import datetime
from datetime import datetime, timedelta
import getpass
import traceback
import glob
Expand Down Expand Up @@ -775,14 +775,23 @@ def get_backups(self, include=[], exclude=[]):

return backups

def clean_backups(self, keep=10, keep_physical = False):
def clean_backups(self, keep, keep_days, keep_physical = False):
_backups = self.get_backups()
backup_ids = [b for b in _backups.keys()]
backup_ids.sort()
index = len(backup_ids)-keep
for backup_id in backup_ids[:index]:

if keep:
index = len(backup_ids)-keep
tbr = backup_ids[:index]

if keep_days:
cutoff = (datetime.now()- timedelta(days=keep_days)).strftime("%Y%m%d%H%M%S")
print cutoff
tbr = [bid for bid in backup_ids if bid < cutoff]

for backup_id in tbr:
print 'Removing backup {0}'.format(backup_id)
self.remove_backup(backup_id, keep_physical)
#self.remove_backup(backup_id, keep_physical)

def remove_backup(self, backup_id, keep_physical = False):
with self._get_deployment_lock():
Expand All @@ -794,7 +803,7 @@ def worker(m):

def backup(self, include=[], exclude=[]):
self.evaluate_active(include, exclude) # unnecessary?
backup_id = datetime.now().strftime("%Y%m%d%H%M%S");
backup_id = datetime.now().strftime("%Y%m%d%H%M%S")

def worker(m):
if not should_do(m, include, exclude): return
Expand Down
9 changes: 7 additions & 2 deletions scripts/nixops
Expand Up @@ -292,8 +292,12 @@ def print_backups(depl, backups):


def op_clean_backups():
if args.keep and args.keep_days:
raise Exception("Combining of --keep and --keep-days arguments are not possible, please use one.")
if not (args.keep or args.keep_days):
raise Exception("Please specify at least --keep or --keep-days arguments.")
depl = open_deployment()
depl.clean_backups(args.keep, args.keep_physical)
depl.clean_backups(args.keep, args.keep_days, args.keep_physical)


def op_remove_backup():
Expand Down Expand Up @@ -877,7 +881,8 @@ subparser.add_argument('--keep-physical', dest="keep_physical", default=False, a

subparser = add_subparser('clean-backups', help='remove old backups')
subparser.set_defaults(op=op_clean_backups)
subparser.add_argument('--keep', dest="keep", type=int, default=10, help='number of backups to keep around')
subparser.add_argument('--keep', dest="keep", type=int, help='number of backups to keep around')
subparser.add_argument('--keep-days', metavar="N", dest="keep_days", type=int, help='keep backups newer than N days')
subparser.add_argument('--keep-physical', dest="keep_physical", default=False, action="store_true", help='do not remove the physical backups, only remove backups from nixops state')

subparser = add_subparser('restore', help='restore machines based on snapshots of persistent disks in network (currently EC2-only)')
Expand Down

0 comments on commit 2bb6205

Please sign in to comment.