]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.maintenance/src/org/argeo/maintenance/backup/vfs/SimpleBackupPurge.java
Merge branch 'master' of https://github.com/argeo/argeo-commons.git
[lgpl/argeo-commons.git] / org.argeo.maintenance / src / org / argeo / maintenance / backup / vfs / 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.maintenance.backup.vfs;
17
18 import java.text.DateFormat;
19 import java.time.Period;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22 import java.util.Date;
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.commons.vfs2.FileObject;
29 import org.apache.commons.vfs2.FileSystemManager;
30 import org.apache.commons.vfs2.FileSystemOptions;
31 import org.apache.commons.vfs2.Selectors;
32 import org.argeo.maintenance.MaintenanceException;
33
34 /** Simple backup purge which keeps backups only for a given number of days */
35 public class SimpleBackupPurge implements BackupPurge {
36 private final static Log log = LogFactory.getLog(SimpleBackupPurge.class);
37
38 private Integer daysKept = 30;
39
40 @Override
41 public void purge(FileSystemManager fileSystemManager, String base, String name, DateFormat dateFormat,
42 FileSystemOptions opts) {
43 try {
44 ZonedDateTime nowDt = ZonedDateTime.now();
45 FileObject baseFo = fileSystemManager.resolveFile(base + '/' + name, opts);
46
47 SortedMap<ZonedDateTime, FileObject> toDelete = new TreeMap<ZonedDateTime, 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 ZonedDateTime backupDt = ZonedDateTime.ofInstant(backupDate.toInstant(), ZoneId.systemDefault());
59 Period sinceThen = Period.between(backupDt.toLocalDate(), nowDt.toLocalDate());
60 // 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 ZonedDateTime lastBackupDt = toDelete.firstKey();
72 FileObject keptFo = toDelete.remove(lastBackupDt);
73 log.warn("Backup " + keptFo + " kept although it is older than " + daysKept + " days.");
74 }
75
76 // delete old backups
77 for (FileObject backupFo : toDelete.values()) {
78 backupFo.delete(Selectors.SELECT_ALL);
79 if (log.isDebugEnabled())
80 log.debug("Deleted backup " + backupFo);
81 }
82 } catch (Exception e) {
83 throw new MaintenanceException("Could not purge previous backups", e);
84 }
85
86 }
87
88 }