First step towards a new, improved cumulus front-end.
[cumulus.git] / python / cumulus / main.py
1 # Cumulus: Smart Filesystem Backup to Dumb Servers
2 #
3 # Copyright (C) 2012  Google Inc.
4 # Written by Michael Vrable <mvrable@cs.ucsd.edu>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 """The Python-based Cumulus script.
21
22 This implements maintenance functions and is a wrapper around the C++
23 cumulus-backup program.
24 """
25
26 import re
27 import sys
28
29 import cumulus
30 from cumulus import cmd_util
31 from cumulus import config
32
33 class FakeOptions:
34     pass
35
36 def prune_backups(backup_config, scheme):
37     store = cumulus.LowlevelDataStore(backup_config.get_global("dest"))
38     snapshot_re = re.compile(r"^(.*)-(.*)$")
39     retention = backup_config.get_retention_for_scheme(scheme)
40     expired_snapshots = []
41     for snapshot in sorted(store.list_snapshots()):
42         m = snapshot_re.match(snapshot)
43         if m.group(1) != scheme: continue
44         timestamp = m.group(2)
45         keep = retention.consider_snapshot(timestamp)
46         if not keep:
47             expired_snapshots.append(snapshot)
48     # The most recent snapshot is never removed.
49     if expired_snapshots: expired_snapshots.pop()
50     print expired_snapshots
51
52     # TODO: Clean up the expiration part...
53     for snapshot in expired_snapshots:
54         store.store.delete("snapshot", "snapshot-%s.lbs" % snapshot)
55
56     print "Collecting garbage..."
57     options = FakeOptions()
58     options.store = backup_config.get_global("dest")
59     options.dry_run = False
60     cmd_util.options = options
61     cmd_util.cmd_garbage_collect([])
62
63 def main(argv):
64     backup_config = config.CumulusConfig(argv[1])
65     for scheme in backup_config.backup_schemes():
66         print scheme
67         prune_backups(backup_config, scheme)
68
69 if __name__ == "__main__":
70     main(sys.argv)