}
// 2. init filestore
FileStore backup = new FileStore(destination, MAX_FILE_SIZE, false);
try {
Journal journal = backup.getJournal("root");
SegmentNodeState state = new SegmentNodeState(
backup.getWriter().getDummySegment(), journal.getHead());
SegmentNodeBuilder builder = state.builder();
String beforeCheckpoint = state.getString("checkpoint");
if (beforeCheckpoint == null) {
// 3.1 no stored checkpoint, so do the initial full backup
builder.setChildNode("root", current);
} else {
// 3.2 try to retrieve the previously backed up checkpoint
NodeState before = store.retrieve(beforeCheckpoint);
if (before != null) {
// the previous checkpoint is no longer available,
// so use the backed up state as the basis of the
// incremental backup diff
before = state.getChildNode("root");
}
current.compareAgainstBaseState(
before, new ApplyDiff(builder.child("root")));
}
builder.setProperty("checkpoint", checkpoint);
// 4. commit the backup
journal.setHead(
state.getRecordId(), builder.getNodeState().getRecordId());
} finally {
backup.close();
}