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