+ tss = new TarSegmentStore(backup_dest);
+ block_buf = new char[LBS_BLOCK_SIZE];
+
+ /* Store the time when the backup started, so it can be included in the
+ * snapshot name. */
+ time_t now;
+ struct tm time_buf;
+ char desc_buf[256];
+ time(&now);
+ localtime_r(&now, &time_buf);
+ strftime(desc_buf, sizeof(desc_buf), "%Y%m%dT%H%M%S", &time_buf);
+
+ /* Open the local database which tracks all objects that are stored
+ * remotely, for efficient incrementals. Provide it with the name of this
+ * snapshot. */
+ 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);
+
+ /* Initialize the stat cache, for skipping over unchanged files. */
+ statcache = new StatCache;
+ statcache->Open(localdb_dir.c_str(), desc_buf);
+
+ scanfile(".", false);
+
+ metadata_flush();
+ const string md = metadata_root.str();
+
+ LbsObject *root = new LbsObject;
+ root->set_group("metadata");
+ root->set_data(md.data(), md.size());
+ root->write(tss);
+ root->checksum();
+ segment_list.insert(root->get_ref().get_segment());
+
+ string backup_root = root->get_ref().to_string();
+ delete root;
+
+ db->Close();
+
+ statcache->Close();
+ delete statcache;
+
+ tss->sync();
+ tss->dump_stats();
+ delete tss;
+
+ /* Write a backup descriptor file, which says which segments are needed and
+ * where to start to restore this snapshot. The filename is based on the
+ * current time. */
+ string desc_filename = backup_dest + "/snapshot-";
+ if (backup_scheme.size() > 0)
+ desc_filename += backup_scheme + "-";
+ desc_filename = desc_filename + desc_buf + ".lbs";
+ std::ofstream descriptor(desc_filename.c_str());
+
+ descriptor << "Format: LBS Snapshot v0.1\n";
+ descriptor << "Producer: LBS " << lbs_version << "\n";
+ strftime(desc_buf, sizeof(desc_buf), "%Y-%m-%d %H:%M:%S %z", &time_buf);
+ descriptor << "Date: " << desc_buf << "\n";
+ if (backup_scheme.size() > 0)
+ descriptor << "Scheme: " << backup_scheme << "\n";
+ descriptor << "Root: " << backup_root << "\n";
+
+ descriptor << "Segments:\n";
+ for (std::set<string>::iterator i = segment_list.begin();
+ i != segment_list.end(); ++i) {
+ descriptor << " " << *i << "\n";
+ }