1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
package com.keuin.kbackupfabric.backup.incremental.manager;
import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2;
import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionIterator;
import com.keuin.kbackupfabric.backup.incremental.ObjectElement;
import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier;
import com.keuin.kbackupfabric.config.KBackupConfig;
import com.keuin.kbackupfabric.util.FilesystemUtil;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.keuin.kbackupfabric.util.cow.FileCopier;
import com.keuin.kbackupfabric.util.cow.FileCowCopier;
import com.keuin.kbackupfabric.util.cow.FileEagerCopier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
import static org.apache.commons.io.FileUtils.forceDelete;
/**
* Managing the base storing all collection objects.
*/
public class IncrementalBackupStorageManager {
private final Logger logger = Logger.getLogger(IncrementalBackupStorageManager.class.getName());
private final Path backupStorageBase;
private final Logger LOGGER = Logger.getLogger(IncrementalBackupStorageManager.class.getName());
private FileCopier copier;
public IncrementalBackupStorageManager(Path backupStorageBase) {
this.backupStorageBase = backupStorageBase;
if (KBackupConfig.getInstance().getIncbakCow()) {
// try to use cow copier, if failed, fallback to normal copier
try {
this.copier = FileCowCopier.getInstance();
} catch (Exception | UnsatisfiedLinkError ex) {
PrintUtil.error("Failed to initialize kbackup-cow: " + ex + ex.getMessage());
this.copier = new FileEagerCopier();
}
} else {
this.copier = new FileEagerCopier();
}
if (this.copier.isCow()) {
PrintUtil.info("Copy-on-write is enabled");
} else {
PrintUtil.info("Copy-on-write is disabled");
}
}
/**
* Check whether the storage contains a copy of file with given identifier.
*
* @param identifier the identifier.
* @return whether the file exists.
*/
public boolean contains(@NotNull ObjectIdentifier identifier) {
Objects.requireNonNull(identifier);
return new File(backupStorageBase.toFile(), identifier.getIdentification()).isFile();
}
/**
* Check whether the storage contains all files in the given collection.
*
* @param collection the collection.
* @return whether all files exist.
*/
public boolean contains(@NotNull ObjectCollection2 collection) {
Objects.requireNonNull(collection);
for (ObjectCollectionIterator it = new ObjectCollectionIterator(collection); it.hasNext(); ) {
ObjectElement ele = it.next();
if (!contains(ele.getIdentifier()))
return false;
}
return true;
}
/**
* Add an object collection to storage base and copy files to the storage.
*
* @param collection the collection.
* @return objects copied to the base.
* @throws IOException I/O error.
*/
public @Nullable
IncCopyResult addObjectCollection(ObjectCollection2 collection, File collectionBasePath) throws IOException {
if (!backupStorageBase.toFile().isDirectory()) {
if (!backupStorageBase.toFile().mkdirs())
throw new IOException("Backup storage base directory does not exist, and failed to create it.");
}
Objects.requireNonNull(collection);
Objects.requireNonNull(collectionBasePath);
IncCopyResult copyCount = IncCopyResult.ZERO;
// copy sub files
for (Map.Entry<String, ObjectElement> entry : collection.getElementMap().entrySet()) {
File copyDestination = new File(backupStorageBase.toFile(), entry.getValue().getIdentifier().getIdentification());
File copySourceFile = new File(collectionBasePath.getAbsolutePath(), entry.getKey());
final long fileBytes = FilesystemUtil.getFileSizeBytes(copySourceFile.getAbsolutePath());
if (!contains(entry.getValue())) {
// element does not exist. copy.
logger.fine("Copy new file `" + copySourceFile.getName() + "`.");
copier.copy(copyDestination.getAbsolutePath(), copySourceFile.getAbsolutePath());
copyCount = copyCount.addWith(new IncCopyResult(1, 1, fileBytes, fileBytes));
} else {
// element exists (file reused). Just update the stat info
copyCount = copyCount.addWith(new IncCopyResult(1, 0, 0, fileBytes));
}
}
//copy sub dirs recursively
for (Map.Entry<String, ObjectCollection2> entry : collection.getSubCollectionMap().entrySet()) {
File newBase = new File(collectionBasePath, entry.getKey());
copyCount = copyCount.addWith(addObjectCollection(entry.getValue(), newBase));
}
return copyCount;
}
/**
* Delete all files in the specific collection, from the storage base.
*
* @param collection the collection containing files to be deleted.
* @return files deleted
*/
public int deleteObjectCollection(ObjectCollection2 collection) {
return deleteObjectCollection(collection, Collections.emptySet());
}
/**
* Delete a collection from the storage base, optionally preserving files used by other backups.
*
* @param collection the collection containing files to be deleted.
* @param otherExistingCollections other collections (not to be deleted) in this base. Files exist in these collections will not be deleted.
* @return files deleted
*/
public int deleteObjectCollection(ObjectCollection2 collection,
Iterable<ObjectCollection2> otherExistingCollections) {
// TODO: test this
Iterator<ObjectElement> iter = new ObjectCollectionIterator(collection);
Set<ObjectIdentifier> identifierSet = new HashSet<>();
iter.forEachRemaining(ele -> identifierSet.add(ele.getIdentifier()));
otherExistingCollections.forEach(col -> new ObjectCollectionIterator(col)
.forEachRemaining(ele -> identifierSet.remove(ele.getIdentifier())));
int deleteCount = 0;
for (ObjectIdentifier id : identifierSet) {
Objects.requireNonNull(id);
File file = new File(backupStorageBase.toFile(), id.getIdentification());
if (file.exists()) {
if (file.delete())
++deleteCount;
else
LOGGER.warning("Failed to delete unused file " + file.getName());
}
}
return deleteCount;
}
/**
* Restore an object collection from the storage base. i.e., restore the save from backup storage.
*
* @param collection the collection to be restored.
* @param collectionBasePath save path of the collection.
* @return objects restored from the base.
* @throws IOException I/O Error.
*/
public int restoreObjectCollection(ObjectCollection2 collection, File collectionBasePath) throws IOException {
Objects.requireNonNull(collection);
Objects.requireNonNull(collectionBasePath);
int copyCount = 0;
// touch directory
if (!collectionBasePath.exists()) {
int retryCounter = 0;
boolean success = false;
while (retryCounter++ < 5) {
if (collectionBasePath.mkdirs()) {
success = true;
break;
}
}
if (!success) {
throw new IOException("Failed to create directory " + collectionBasePath.getAbsolutePath());
}
}
// copy sub files
for (Map.Entry<String, ObjectElement> entry : collection.getElementMap().entrySet()) {
File copySource = new File(backupStorageBase.toFile(), entry.getValue().getIdentifier().getIdentification());
File copyTarget = new File(collectionBasePath.getAbsolutePath(), entry.getKey());
if (!contains(entry.getValue())) {
throw new IOException(String.format("File %s is missing in the backup storage. Cannot restore.", copySource.getName()));
}
if (copyTarget.exists()) {
boolean successDeleting = false;
for (int i = 0; i < 5; ++i) {
try {
forceDelete(copyTarget);
successDeleting = true;
break;
} catch (FileNotFoundException ignored) {
break;
} catch (IOException e) {
PrintUtil.error(String.format("Failed to delete file %s, retry.", copyTarget.getName()));
}
}
if (!successDeleting) {
String msg = String.format("Failed to delete file %s.", copyTarget.getName());
PrintUtil.error(msg);
throw new IOException(msg);
}
}
copier.copy(copyTarget.getAbsolutePath(), copySource.getAbsolutePath());
++copyCount;
}
//copy sub dirs recursively
for (Map.Entry<String, ObjectCollection2> entry : collection.getSubCollectionMap().entrySet()) {
File newBase = new File(collectionBasePath, entry.getKey());
copyCount += restoreObjectCollection(entry.getValue(), newBase);
}
return copyCount;
}
/**
* Check if the backup base contains given element.
*
* @param objectElement the element.
* @return true or false.
*/
private boolean contains(@NotNull ObjectElement objectElement) {
// This can be extended to use more variants of hash functions and combinations of other attributes (such as file size)
Objects.requireNonNull(objectElement);
return contains(objectElement.getIdentifier());
}
}
|