]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/backup/SimpleBackupPurge.java
Fix automated Kerberos config
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / backup / SimpleBackupPurge.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.internal.backup;
17
18 import java.text.DateFormat;
19 import java.util.Date;
20 import java.util.SortedMap;
21 import java.util.TreeMap;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystemManager;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.Selectors;
29 import org.argeo.cms.CmsException;
30 import org.joda.time.DateTime;
31 import org.joda.time.Period;
32
33 /** Simple backup purge which keeps backups only for a given number of days */
34 public class SimpleBackupPurge implements BackupPurge {
35 private final static Log log = LogFactory.getLog(SimpleBackupPurge.class);
36
37 private Integer daysKept = 30;
38
39 @Override
40 public void purge(FileSystemManager fileSystemManager, String base,
41 String name, DateFormat dateFormat, FileSystemOptions opts) {
42 try {
43 DateTime nowDt = new DateTime();
44 FileObject baseFo = fileSystemManager.resolveFile(
45 base + '/' + name, opts);
46
47 SortedMap<DateTime, FileObject> toDelete = new TreeMap<DateTime, FileObject>();
48 int backupCount = 0;
49
50 // make sure base dir exists
51 baseFo.createFolder();
52
53 // scan backups and list those which should be deleted
54 for (FileObject backupFo : baseFo.getChildren()) {
55 String backupName = backupFo.getName().getBaseName();
56 Date backupDate = dateFormat.parse(backupName);
57 backupCount++;
58
59 DateTime backupDt = new DateTime(backupDate.getTime());
60 Period sinceThen = new Period(backupDt, nowDt);
61 int days = sinceThen.getDays();
62 // int days = sinceThen.getMinutes();
63 if (days > daysKept) {
64 toDelete.put(backupDt, backupFo);
65 }
66 }
67
68 if (toDelete.size() != 0 && toDelete.size() == backupCount) {
69 // all backups would be deleted
70 // but we want to keep at least one
71 DateTime lastBackupDt = toDelete.firstKey();
72 FileObject keptFo = toDelete.remove(lastBackupDt);
73 log.warn("Backup " + keptFo
74 + " kept although it is older than " + daysKept
75 + " days.");
76 }
77
78 // delete old backups
79 for (FileObject backupFo : toDelete.values()) {
80 backupFo.delete(Selectors.SELECT_ALL);
81 if (log.isDebugEnabled())
82 log.debug("Deleted backup " + backupFo);
83 }
84 } catch (Exception e) {
85 throw new CmsException("Could not purge previous backups", e);
86 }
87
88 }
89
90 }