#include "metadata.h"
#include "store.h"
#include "sha1.h"
-#include "statcache.h"
#include "util.h"
using std::list;
* invocations to help in creating incremental snapshots. */
LocalDb *db;
-/* Stat cache, which stored data locally to speed the backup process by quickly
- * skipping files which have not changed. */
-StatCache *statcache;
-
/* Keep track of all segments which are needed to reconstruct the snapshot. */
std::set<string> segment_list;
+/* Snapshot intent: 1=daily, 7=weekly, etc. This is not used directly, but is
+ * stored in the local database and can help guide segment cleaning and
+ * snapshot expiration policies. */
+double snapshot_intent = 1.0;
+
/* Selection of files to include/exclude in the snapshot. */
std::list<string> includes; // Paths in which files should be saved
std::list<string> excludes; // Paths which will not be saved
* re-reading the entire contents. */
bool cached = false;
- if (statcache->Find(path, &stat_buf)) {
+ if (metawriter->find(path) && metawriter->is_unchanged(&stat_buf)) {
cached = true;
- const list<ObjectReference> &blocks = statcache->get_blocks();
+ list<ObjectReference> blocks = metawriter->get_blocks();
/* If any of the blocks in the object have been expired, then we should
* fall back to fully reading in the file. */
/* If everything looks okay, use the cached information */
if (cached) {
- file_info["checksum"] = statcache->get_checksum();
+ file_info["checksum"] = metawriter->get_checksum();
for (list<ObjectReference>::const_iterator i = blocks.begin();
i != blocks.end(); ++i) {
const ObjectReference &ref = *i;
object_list.push_back(ref.to_string());
- segment_list.insert(ref.get_segment());
+ if (ref.is_normal())
+ add_segment(ref.get_segment());
db->UseObject(ref);
}
size = stat_buf.st_size;
hash.process(block_buf, bytes);
+ // Sparse file processing: if we read a block of all zeroes, encode
+ // that explicitly.
+ bool all_zero = true;
+ for (int i = 0; i < bytes; i++) {
+ if (block_buf[i] != 0) {
+ all_zero = false;
+ break;
+ }
+ }
+
// Either find a copy of this block in an already-existing segment,
// or index it so it can be re-used in the future
double block_age = 0.0;
+ ObjectReference ref;
+
SHA1Checksum block_hash;
block_hash.process(block_buf, bytes);
string block_csum = block_hash.checksum_str();
- ObjectReference ref = db->FindObject(block_csum, bytes);
+
+ if (all_zero) {
+ ref = ObjectReference(ObjectReference::REF_ZERO);
+ ref.set_range(0, bytes);
+ } else {
+ ref = db->FindObject(block_csum, bytes);
+ }
// Store a copy of the object if one does not yet exist
- if (ref.get_segment().size() == 0) {
+ if (ref.is_null()) {
LbsObject *o = new LbsObject;
int object_group;
o->write(tss);
ref = o->get_ref();
db->StoreObject(ref, block_csum, bytes, block_age);
+ ref.set_range(0, bytes);
delete o;
}
object_list.push_back(ref.to_string());
- segment_list.insert(ref.get_segment());
+ if (ref.is_normal())
+ add_segment(ref.get_segment());
db->UseObject(ref);
size += bytes;
if (status != NULL)
printf(" [%s]\n", status);
- statcache->Save(path, &stat_buf, file_info["checksum"], object_list);
-
string blocklist = "";
for (list<string>::iterator i = object_list.begin();
i != object_list.end(); ++i) {
ssize_t len;
printf("%s\n", path.c_str());
+ metawriter->find(path);
- if (metawriter->find(path)) {
- ObjectReference *r = metawriter->old_ref();
- if (r != NULL) {
- string s = r->to_string();
- printf(" cached at %s\n", s.c_str());
- delete r;
- }
- }
-
- file_info["path"] = uri_encode(path);
+ file_info["name"] = uri_encode(path);
file_info["mode"] = encode_int(stat_buf.st_mode & 07777, 8);
file_info["ctime"] = encode_int(stat_buf.st_ctime);
file_info["mtime"] = encode_int(stat_buf.st_mtime);
file_info["user"] = encode_int(stat_buf.st_uid);
file_info["group"] = encode_int(stat_buf.st_gid);
+ time_t now = time(NULL);
+ if (now - stat_buf.st_ctime < 30 || now - stat_buf.st_mtime < 30)
+ if ((stat_buf.st_mode & S_IFMT) != S_IFDIR)
+ file_info["volatile"] = "1";
+
struct passwd *pwd = getpwuid(stat_buf.st_uid);
if (pwd != NULL) {
file_info["user"] += " (" + uri_encode(pwd->pw_name) + ")";
if (file_size != stat_buf.st_size) {
fprintf(stderr, "Warning: Size of %s changed during reading\n",
path.c_str());
+ file_info["volatile"] = "1";
}
break;
" (defaults to \".bz2\")\n"
" --signature-filter=COMMAND\n"
" program though which to filter descriptor\n"
- " --scheme=NAME optional name for this snapshot\n",
+ " --scheme=NAME optional name for this snapshot\n"
+ " --intent=FLOAT intended backup type: 1=daily, 7=weekly, ...\n"
+ " (defaults to \"1\")\n"
+ " --full-metadata do not re-use metadata from previous backups\n",
lbs_version, program
);
}
{"dest", 1, 0, 0}, // 4
{"scheme", 1, 0, 0}, // 5
{"signature-filter", 1, 0, 0}, // 6
+ {"intent", 1, 0, 0}, // 7
+ {"full-metadata", 0, 0, 0}, // 8
{NULL, 0, 0, 0},
};
case 6: // --signature-filter
signature_filter = optarg;
break;
+ case 7: // --intent
+ snapshot_intent = atof(optarg);
+ if (snapshot_intent <= 0)
+ snapshot_intent = 1;
+ break;
+ case 8: // --full-metadata
+ flag_full_metadata = true;
+ break;
default:
fprintf(stderr, "Unhandled long option!\n");
return 1;
string database_path = localdb_dir + "/localdb.sqlite";
db = new LocalDb;
db->Open(database_path.c_str(), desc_buf,
- backup_scheme.size() ? backup_scheme.c_str() : NULL);
+ backup_scheme.size() ? backup_scheme.c_str() : NULL,
+ snapshot_intent);
tss = new TarSegmentStore(backup_dest, db);
/* Initialize the stat cache, for skipping over unchanged files. */
- statcache = new StatCache;
- statcache->Open(localdb_dir.c_str(), desc_buf,
- backup_scheme.size() ? backup_scheme.c_str() : NULL);
-
metawriter = new MetadataWriter(tss, localdb_dir.c_str(), desc_buf,
backup_scheme.size()
? backup_scheme.c_str()
add_segment(root_ref.get_segment());
string backup_root = root_ref.to_string();
- statcache->Close();
- delete statcache;
-
delete metawriter;
tss->sync();
fprintf(descriptor, "Date: %s\n", desc_buf);
if (backup_scheme.size() > 0)
fprintf(descriptor, "Scheme: %s\n", backup_scheme.c_str());
+ fprintf(descriptor, "Backup-Intent: %g\n", snapshot_intent);
fprintf(descriptor, "Root: %s\n", backup_root.c_str());
SHA1Checksum checksum_csum;