#!/usr/bin/python # -*- coding: utf-8 -*- """Provide NEXT3 filesystem automatic snapshot creation and cleanup Copyright 2011, Active Systems Danel Ahman This software may be freely redistributed under the terms of the GNU general public license. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ from sys import exit from sys import argv from getopt import getopt import commands import datetime def main(): try: options, args = getopt(argv[1:], 'dt:', ['time=', 'debug']) except Exception, err: print str(err) exit(2) debug = False delete_days = 30 for o, a in options: if o in ("-t", "--time"): delete_days = int(a) elif o in ("-d", "--debug"): debug = True snapshot_list = get_snapshot_list() for (snapshot_name, snapshot_time) in snapshot_list: try: if ((snapshot_time.today() - snapshot_time).days >= delete_days): if (debug): print "Removing NEXT3 snapshot: %s" % (snapshot_name) (status, output) = commands.getstatusoutput("next3 remove %s" % (snapshot_name)) (status, output) = commands.getstatusoutput("next3 remove %s" % (snapshot_name)) except: pass if (debug): print "Creating new NEXT3 snapshot" (status, output) = commands.getstatusoutput("next3 take") return 0 def get_snapshot_list(): """Get the list of NEXT3 snapshots""" snapshot_list = [] (status, output) = commands.getstatusoutput("next3 stat") output = output[output.find("Snapshots list:"):] output = output.split("\n") output = output[3:] output = output[:len(output)-1] for row in output: try: items = row.split() filename = items[7] snapshotname = filename[filename.rfind("/")+1:] datetimestr = snapshotname[4:] datetimearray = datetimestr.split("-") datearray = datetimearray[0].split(".") timearray = datetimearray[1].split(".") time = datetime.datetime(int(datearray[0]), int(datearray[1]), int(datearray[2]), int(timearray[0]), int(timearray[1]), int(timearray[2])) snapshot_list.append((snapshotname, time)) except: pass return snapshot_list if __name__=='__main__': main()