1 /* Recursively descend the filesystem and visit each file. */
13 #include <sys/sysmacros.h>
14 #include <sys/types.h>
31 #include "statcache.h"
39 /* Version information. This will be filled in by the Makefile. */
41 #define LBS_VERSION Unknown
43 #define LBS_STRINGIFY(s) LBS_STRINGIFY2(s)
44 #define LBS_STRINGIFY2(s) #s
45 static const char lbs_version[] = LBS_STRINGIFY(LBS_VERSION);
47 static TarSegmentStore *tss = NULL;
48 static MetadataWriter *metawriter = NULL;
50 /* Buffer for holding a single block of data read from a file. */
51 static const size_t LBS_BLOCK_SIZE = 1024 * 1024;
52 static char *block_buf;
54 /* Local database, which tracks objects written in this and previous
55 * invocations to help in creating incremental snapshots. */
58 /* Stat cache, which stored data locally to speed the backup process by quickly
59 * skipping files which have not changed. */
62 /* Keep track of all segments which are needed to reconstruct the snapshot. */
63 std::set<string> segment_list;
65 /* Selection of files to include/exclude in the snapshot. */
66 std::list<string> includes; // Paths in which files should be saved
67 std::list<string> excludes; // Paths which will not be saved
68 std::list<string> searches; // Directories we don't want to save, but
69 // do want to descend searching for data
72 bool relative_paths = true;
74 /* Ensure that the given segment is listed as a dependency of the current
76 void add_segment(const string& segment)
78 segment_list.insert(segment);
81 /* Read data from a file descriptor and return the amount of data read. A
82 * short read (less than the requested size) will only occur if end-of-file is
84 ssize_t file_read(int fd, char *buf, size_t maxlen)
86 size_t bytes_read = 0;
89 ssize_t res = read(fd, buf, maxlen);
93 fprintf(stderr, "error reading file: %m\n");
95 } else if (res == 0) {
107 /* Read the contents of a file (specified by an open file descriptor) and copy
108 * the data to the store. Returns the size of the file (number of bytes
109 * dumped), or -1 on error. */
110 int64_t dumpfile(int fd, dictionary &file_info, const string &path,
111 struct stat& stat_buf)
114 list<string> object_list;
115 const char *status = NULL; /* Status indicator printed out */
117 /* Look up this file in the old stat cache, if we can. If the stat
118 * information indicates that the file has not changed, do not bother
119 * re-reading the entire contents. */
122 if (statcache->Find(path, &stat_buf)) {
124 const list<ObjectReference> &blocks = statcache->get_blocks();
126 /* If any of the blocks in the object have been expired, then we should
127 * fall back to fully reading in the file. */
128 for (list<ObjectReference>::const_iterator i = blocks.begin();
129 i != blocks.end(); ++i) {
130 const ObjectReference &ref = *i;
131 if (!db->IsAvailable(ref)) {
138 /* If everything looks okay, use the cached information */
140 file_info["checksum"] = statcache->get_checksum();
141 for (list<ObjectReference>::const_iterator i = blocks.begin();
142 i != blocks.end(); ++i) {
143 const ObjectReference &ref = *i;
144 object_list.push_back(ref.to_string());
145 segment_list.insert(ref.get_segment());
148 size = stat_buf.st_size;
152 /* If the file is new or changed, we must read in the contents a block at a
157 ssize_t bytes = file_read(fd, block_buf, LBS_BLOCK_SIZE);
161 fprintf(stderr, "Backup contents for %s may be incorrect\n",
166 hash.process(block_buf, bytes);
168 // Either find a copy of this block in an already-existing segment,
169 // or index it so it can be re-used in the future
170 double block_age = 0.0;
171 SHA1Checksum block_hash;
172 block_hash.process(block_buf, bytes);
173 string block_csum = block_hash.checksum_str();
174 ObjectReference ref = db->FindObject(block_csum, bytes);
176 // Store a copy of the object if one does not yet exist
177 if (ref.get_segment().size() == 0) {
178 LbsObject *o = new LbsObject;
181 /* We might still have seen this checksum before, if the object
182 * was stored at some time in the past, but we have decided to
183 * clean the segment the object was originally stored in
184 * (FindObject will not return such objects). When rewriting
185 * the object contents, put it in a separate group, so that old
186 * objects get grouped together. The hope is that these old
187 * objects will continue to be used in the future, and we
188 * obtain segments which will continue to be well-utilized.
189 * Additionally, keep track of the age of the data by looking
190 * up the age of the block which was expired and using that
191 * instead of the current time. */
192 if (db->IsOldObject(block_csum, bytes,
193 &block_age, &object_group)) {
194 if (object_group == 0) {
195 o->set_group("data");
198 sprintf(group, "compacted-%d", object_group);
204 o->set_group("data");
208 o->set_data(block_buf, bytes);
211 db->StoreObject(ref, block_csum, bytes, block_age);
215 object_list.push_back(ref.to_string());
216 segment_list.insert(ref.get_segment());
224 file_info["checksum"] = hash.checksum_str();
228 printf(" [%s]\n", status);
230 statcache->Save(path, &stat_buf, file_info["checksum"], object_list);
232 string blocklist = "";
233 for (list<string>::iterator i = object_list.begin();
234 i != object_list.end(); ++i) {
235 if (i != object_list.begin())
239 file_info["data"] = blocklist;
244 /* Dump a specified filesystem object (file, directory, etc.) based on its
245 * inode information. If the object is a regular file, an open filehandle is
247 void dump_inode(const string& path, // Path within snapshot
248 const string& fullpath, // Path to object in filesystem
249 struct stat& stat_buf, // Results of stat() call
250 int fd) // Open filehandle if regular file
253 dictionary file_info;
257 printf("%s\n", path.c_str());
259 file_info["mode"] = encode_int(stat_buf.st_mode & 07777, 8);
260 file_info["ctime"] = encode_int(stat_buf.st_ctime);
261 file_info["mtime"] = encode_int(stat_buf.st_mtime);
262 file_info["user"] = encode_int(stat_buf.st_uid);
263 file_info["group"] = encode_int(stat_buf.st_gid);
265 struct passwd *pwd = getpwuid(stat_buf.st_uid);
267 file_info["user"] += " (" + uri_encode(pwd->pw_name) + ")";
270 struct group *grp = getgrgid(stat_buf.st_gid);
272 file_info["group"] += " (" + uri_encode(grp->gr_name) + ")";
275 if (stat_buf.st_nlink > 1 && (stat_buf.st_mode & S_IFMT) != S_IFDIR) {
276 file_info["links"] = encode_int(stat_buf.st_nlink);
279 file_info["inode"] = encode_int(major(stat_buf.st_dev))
280 + "/" + encode_int(minor(stat_buf.st_dev))
281 + "/" + encode_int(stat_buf.st_ino);
285 switch (stat_buf.st_mode & S_IFMT) {
294 inode_type = ((stat_buf.st_mode & S_IFMT) == S_IFBLK) ? 'b' : 'c';
295 file_info["device"] = encode_int(major(stat_buf.st_rdev))
296 + "/" + encode_int(minor(stat_buf.st_rdev));
301 /* Use the reported file size to allocate a buffer large enough to read
302 * the symlink. Allocate slightly more space, so that we ask for more
303 * bytes than we expect and so check for truncation. */
304 buf = new char[stat_buf.st_size + 2];
305 len = readlink(fullpath.c_str(), buf, stat_buf.st_size + 1);
307 fprintf(stderr, "error reading symlink: %m\n");
308 } else if (len <= stat_buf.st_size) {
310 file_info["target"] = uri_encode(buf);
311 } else if (len > stat_buf.st_size) {
312 fprintf(stderr, "error reading symlink: name truncated\n");
320 file_size = dumpfile(fd, file_info, path, stat_buf);
321 file_info["size"] = encode_int(file_size);
324 return; // error occurred; do not dump file
326 if (file_size != stat_buf.st_size) {
327 fprintf(stderr, "Warning: Size of %s changed during reading\n",
337 fprintf(stderr, "Unknown inode type: mode=%x\n", stat_buf.st_mode);
341 file_info["type"] = string(1, inode_type);
343 metawriter->add(path, file_info);
346 void scanfile(const string& path, bool include)
350 struct stat stat_buf;
357 true_path = "/" + path;
359 // Set to true if we should scan through the contents of this directory,
360 // but not actually back files up
361 bool scan_only = false;
363 // Check this file against the include/exclude list to see if it should be
365 for (list<string>::iterator i = includes.begin();
366 i != includes.end(); ++i) {
368 printf("Including %s\n", path.c_str());
373 for (list<string>::iterator i = excludes.begin();
374 i != excludes.end(); ++i) {
376 printf("Excluding %s\n", path.c_str());
381 for (list<string>::iterator i = searches.begin();
382 i != searches.end(); ++i) {
384 printf("Scanning %s\n", path.c_str());
389 if (!include && !scan_only)
392 if (lstat(true_path.c_str(), &stat_buf) < 0) {
393 fprintf(stderr, "lstat(%s): %m\n", path.c_str());
397 if ((stat_buf.st_mode & S_IFMT) == S_IFREG) {
398 /* Be paranoid when opening the file. We have no guarantee that the
399 * file was not replaced between the stat() call above and the open()
400 * call below, so we might not even be opening a regular file. We
401 * supply flags to open to to guard against various conditions before
402 * we can perform an lstat to check that the file is still a regular
404 * - O_NOFOLLOW: in the event the file was replaced by a symlink
405 * - O_NONBLOCK: prevents open() from blocking if the file was
407 * We also add in O_NOATIME, since this may reduce disk writes (for
408 * inode updates). However, O_NOATIME may result in EPERM, so if the
409 * initial open fails, try again without O_NOATIME. */
410 fd = open(true_path.c_str(), O_RDONLY|O_NOATIME|O_NOFOLLOW|O_NONBLOCK);
412 fd = open(true_path.c_str(), O_RDONLY|O_NOFOLLOW|O_NONBLOCK);
415 fprintf(stderr, "Unable to open file %s: %m\n", path.c_str());
419 /* Drop the use of the O_NONBLOCK flag; we only wanted that for file
421 flags = fcntl(fd, F_GETFL);
422 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
424 /* Perform the stat call again, and check that we still have a regular
426 if (fstat(fd, &stat_buf) < 0) {
427 fprintf(stderr, "fstat: %m\n");
432 if ((stat_buf.st_mode & S_IFMT) != S_IFREG) {
433 fprintf(stderr, "file is no longer a regular file!\n");
439 dump_inode(path, true_path, stat_buf, fd);
444 // If we hit a directory, now that we've written the directory itself,
445 // recursively scan the directory.
446 if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) {
447 DIR *dir = opendir(true_path.c_str());
450 fprintf(stderr, "Error: %m\n");
455 vector<string> contents;
456 while ((ent = readdir(dir)) != NULL) {
457 string filename(ent->d_name);
458 if (filename == "." || filename == "..")
460 contents.push_back(filename);
465 sort(contents.begin(), contents.end());
467 for (vector<string>::iterator i = contents.begin();
468 i != contents.end(); ++i) {
469 const string& filename = *i;
471 scanfile(filename, include);
473 scanfile(path + "/" + filename, include);
478 /* Include the specified file path in the backups. Append the path to the
479 * includes list, and to ensure that we actually see the path when scanning the
480 * directory tree, add all the parent directories to the search list, which
481 * means we will scan through the directory listing even if the files
482 * themselves are excluded from being backed up. */
483 void add_include(const char *path)
485 printf("Add: %s\n", path);
486 /* Was an absolute path specified? If so, we'll need to start scanning
487 * from the root directory. Make sure that the user was consistent in
488 * providing either all relative paths or all absolute paths. */
489 if (path[0] == '/') {
490 if (includes.size() > 0 && relative_paths == true) {
492 "Error: Cannot mix relative and absolute paths!\n");
496 relative_paths = false;
498 // Skip over leading '/'
500 } else if (relative_paths == false && path[0] != '/') {
501 fprintf(stderr, "Error: Cannot mix relative and absolute paths!\n");
505 includes.push_back(path);
507 /* Split the specified path into directory components, and ensure that we
508 * descend into all the directories along the path. */
509 const char *slash = path;
514 while ((slash = strchr(slash + 1, '/')) != NULL) {
515 string component(path, slash - path);
516 searches.push_back(component);
520 void usage(const char *program)
525 "Usage: %s [OPTION]... --dest=DEST PATHS...\n"
526 "Produce backup snapshot of files in SOURCE and store to DEST.\n"
529 " --dest=PATH path where backup is to be written [REQUIRED]\n"
530 " --exclude=PATH exclude files in PATH from snapshot\n"
531 " --localdb=PATH local backup metadata is stored in PATH\n"
532 " --filter=COMMAND program through which to filter segment data\n"
533 " (defaults to \"bzip2 -c\")\n"
534 " --filter-extension=EXT\n"
535 " string to append to segment files\n"
536 " (defaults to \".bz2\")\n"
537 " --signature-filter=COMMAND\n"
538 " program though which to filter descriptor\n"
539 " --scheme=NAME optional name for this snapshot\n",
544 int main(int argc, char *argv[])
546 string backup_dest = "";
547 string localdb_dir = "";
548 string backup_scheme = "";
549 string signature_filter = "";
552 static struct option long_options[] = {
553 {"localdb", 1, 0, 0}, // 0
554 {"exclude", 1, 0, 0}, // 1
555 {"filter", 1, 0, 0}, // 2
556 {"filter-extension", 1, 0, 0}, // 3
557 {"dest", 1, 0, 0}, // 4
558 {"scheme", 1, 0, 0}, // 5
559 {"signature-filter", 1, 0, 0}, // 6
564 int c = getopt_long(argc, argv, "", long_options, &long_index);
570 switch (long_index) {
572 localdb_dir = optarg;
575 if (optarg[0] != '/')
576 excludes.push_back(optarg);
578 excludes.push_back(optarg + 1);
581 filter_program = optarg;
583 case 3: // --filter-extension
584 filter_extension = optarg;
587 backup_dest = optarg;
590 backup_scheme = optarg;
592 case 6: // --signature-filter
593 signature_filter = optarg;
596 fprintf(stderr, "Unhandled long option!\n");
605 if (optind == argc) {
610 searches.push_back(".");
611 for (int i = optind; i < argc; i++)
612 add_include(argv[i]);
614 if (backup_dest == "") {
616 "Error: Backup destination must be specified with --dest=\n");
621 // Default for --localdb is the same as --dest
622 if (localdb_dir == "") {
623 localdb_dir = backup_dest;
626 // Dump paths for debugging/informational purposes
628 list<string>::const_iterator i;
630 printf("LBS Version: %s\n", lbs_version);
632 printf("--dest=%s\n--localdb=%s\n\n",
633 backup_dest.c_str(), localdb_dir.c_str());
635 printf("Includes:\n");
636 for (i = includes.begin(); i != includes.end(); ++i)
637 printf(" %s\n", i->c_str());
639 printf("Excludes:\n");
640 for (i = excludes.begin(); i != excludes.end(); ++i)
641 printf(" %s\n", i->c_str());
643 printf("Searching:\n");
644 for (i = searches.begin(); i != searches.end(); ++i)
645 printf(" %s\n", i->c_str());
648 block_buf = new char[LBS_BLOCK_SIZE];
650 /* Store the time when the backup started, so it can be included in the
656 localtime_r(&now, &time_buf);
657 strftime(desc_buf, sizeof(desc_buf), "%Y%m%dT%H%M%S", &time_buf);
659 /* Open the local database which tracks all objects that are stored
660 * remotely, for efficient incrementals. Provide it with the name of this
662 string database_path = localdb_dir + "/localdb.sqlite";
664 db->Open(database_path.c_str(), desc_buf,
665 backup_scheme.size() ? backup_scheme.c_str() : NULL);
667 tss = new TarSegmentStore(backup_dest, db);
669 /* Initialize the stat cache, for skipping over unchanged files. */
670 statcache = new StatCache;
671 statcache->Open(localdb_dir.c_str(), desc_buf,
672 backup_scheme.size() ? backup_scheme.c_str() : NULL);
674 metawriter = new MetadataWriter(tss, localdb_dir.c_str(), desc_buf,
676 ? backup_scheme.c_str()
679 scanfile(".", false);
681 ObjectReference root_ref = metawriter->close();
682 add_segment(root_ref.get_segment());
683 string backup_root = root_ref.to_string();
694 /* Write out a checksums file which lists the checksums for all the
695 * segments included in this snapshot. The format is designed so that it
696 * may be easily verified using the sha1sums command. */
697 const char csum_type[] = "sha1";
698 string checksum_filename = backup_dest + "/snapshot-";
699 if (backup_scheme.size() > 0)
700 checksum_filename += backup_scheme + "-";
701 checksum_filename = checksum_filename + desc_buf + "." + csum_type + "sums";
702 FILE *checksums = fopen(checksum_filename.c_str(), "w");
703 if (checksums != NULL) {
704 for (std::set<string>::iterator i = segment_list.begin();
705 i != segment_list.end(); ++i) {
706 string seg_path, seg_csum;
707 if (db->GetSegmentChecksum(*i, &seg_path, &seg_csum)) {
708 const char *raw_checksum = NULL;
709 if (strncmp(seg_csum.c_str(), csum_type,
710 strlen(csum_type)) == 0) {
711 raw_checksum = seg_csum.c_str() + strlen(csum_type);
712 if (*raw_checksum == '=')
718 if (raw_checksum != NULL)
719 fprintf(checksums, "%s *%s\n",
720 raw_checksum, seg_path.c_str());
725 fprintf(stderr, "ERROR: Unable to write checksums file: %m\n");
730 /* Write a backup descriptor file, which says which segments are needed and
731 * where to start to restore this snapshot. The filename is based on the
732 * current time. If a signature filter program was specified, filter the
733 * data through that to give a chance to sign the descriptor contents. */
734 string desc_filename = backup_dest + "/snapshot-";
735 if (backup_scheme.size() > 0)
736 desc_filename += backup_scheme + "-";
737 desc_filename = desc_filename + desc_buf + ".lbs";
739 int descriptor_fd = open(desc_filename.c_str(), O_WRONLY | O_CREAT, 0666);
740 if (descriptor_fd < 0) {
741 fprintf(stderr, "Unable to open descriptor output file: %m\n");
744 pid_t signature_pid = 0;
745 if (signature_filter.size() > 0) {
746 int new_fd = spawn_filter(descriptor_fd, signature_filter.c_str(),
748 close(descriptor_fd);
749 descriptor_fd = new_fd;
751 FILE *descriptor = fdopen(descriptor_fd, "w");
753 fprintf(descriptor, "Format: LBS Snapshot v0.6\n");
754 fprintf(descriptor, "Producer: LBS %s\n", lbs_version);
755 strftime(desc_buf, sizeof(desc_buf), "%Y-%m-%d %H:%M:%S %z", &time_buf);
756 fprintf(descriptor, "Date: %s\n", desc_buf);
757 if (backup_scheme.size() > 0)
758 fprintf(descriptor, "Scheme: %s\n", backup_scheme.c_str());
759 fprintf(descriptor, "Root: %s\n", backup_root.c_str());
761 SHA1Checksum checksum_csum;
762 if (checksum_csum.process_file(checksum_filename.c_str())) {
763 string csum = checksum_csum.checksum_str();
764 fprintf(descriptor, "Checksums: %s\n", csum.c_str());
767 fprintf(descriptor, "Segments:\n");
768 for (std::set<string>::iterator i = segment_list.begin();
769 i != segment_list.end(); ++i) {
770 fprintf(descriptor, " %s\n", i->c_str());
777 waitpid(signature_pid, &status, 0);
779 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
780 throw IOException("Signature filter process error");