Merge branch 'master' of ecbaldwin.net:/home/cnb/dvl/backup/
authorAlan Jack Pippin <ajp@pippin.(none)>
Sat, 12 Nov 2005 21:50:21 +0000 (14:50 -0700)
committerAlan Jack Pippin <ajp@pippin.(none)>
Sat, 12 Nov 2005 21:50:21 +0000 (14:50 -0700)
filedata.cpp
filedata.hpp
main.cpp
scripts/burn-imgs.sh

index 6b14517eec2125bfe80b922101328b0e33fdc364..81ebb588058c503664d9d8b1618d17b684cdd0bb 100644 (file)
@@ -25,6 +25,10 @@ vector<string> split( const string &line, char c, int limit = -1 ) {
   return out;
 }
 
+const FileData::LastBackupCmp FileData::lastbackupcmp = FileData::LastBackupCmp();
+const FileData::SizeCmp       FileData::sizecmp       = FileData::SizeCmp();
+const FileData::NameCmp       FileData::namecmp       = FileData::NameCmp();
+
 ostream &operator<<( ostream &o, const FileData *d) {
   return operator<<( o, *d );
 }
index b339f05f92b2fcc4c425e13c119b56bbb37a8a5b..f98b520e5e8d18eef6d2bf79b3a0c58ab181e0eb 100644 (file)
@@ -29,6 +29,30 @@ class FileData {
     void setFileName(       const std::string &arg ) { filename         = arg; }
     void setLastBackupDate( unsigned long long arg ) { last_backup_date = arg; }
 
+    struct LastBackupCmp {
+      bool operator()( const FileData *a, const FileData *b ) {
+        return a->getLastBackupDate() < b->getLastBackupDate();
+      }
+    };
+
+    struct SizeCmp {
+      bool operator()( const FileData *a, const FileData *b ) {
+        return a->getFileSize() < b->getFileSize();
+      }
+    };
+
+    struct NameCmp {
+      bool operator()( const FileData *a, const FileData *b ) {
+        return cmp( a->getFileName(), b->getFileName() );
+      }
+      private:
+        std::less<std::string> cmp;
+    };
+
+    static const LastBackupCmp lastbackupcmp;
+    static const SizeCmp       sizecmp;
+    static const NameCmp       namecmp;
+
   private:
     FileData( const FileData & );
 
@@ -48,29 +72,9 @@ std::istream &operator>>( std::istream &i, FileData *d );
 std::ostream &operator<<( std::ostream &o, const FileData &d );
 std::istream &operator>>( std::istream &i, FileData &d );
 
-struct FileDataLastBackupCmp {
-  bool operator()( const FileData *a, const FileData *b ) {
-    return a->getLastBackupDate() < b->getLastBackupDate();
-  }
-};
-
-struct FileDataSizeCmp {
-  bool operator()( const FileData *a, const FileData *b ) {
-    return a->getFileSize() < b->getFileSize();
-  }
-};
-
-struct FileDataNameCmp {
-  bool operator()( const FileData *a, const FileData *b ) {
-    return cmp( a->getFileName(), b->getFileName() );
-  }
-  private:
-    std::less<std::string> cmp;
-};
-
 bool needs_backup( const FileData *before, const FileData *after );
 
-typedef std::set<FileData*,FileDataNameCmp> file_set;
-typedef std::vector<FileData*> file_vector;
+typedef std::set<FileData*,FileData::NameCmp> file_set;
+typedef std::vector<FileData*>                file_vector;
 
 #endif
index fb7d522798909ef1d6c90533fed9fec48157adcc..2dd8048575903ac09b9214e56538db323c149158 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -35,7 +35,7 @@ bool copy_until_full( I begin, I end, O out, INT &space ) {
 
     if( blocksize <= space ) {
       space -= blocksize;
-      out = *begin;
+      *out = *begin;
       ++out;
     } else {
       // We missed a file that should be included so the backup is not complete
@@ -62,27 +62,25 @@ void populate_set( istream &in, SET &files ) {
 template<class SET>
 void partition_sets( const SET &current, const SET &old,
                      SET &added, SET &common, SET &old_common, SET &deleted  ) {
-  FileDataNameCmp cmp;
-
   set_difference(   current.begin(), current.end(),
                     old.begin(),     old.end(),
                     inserter( added, added.begin() ),
-                    cmp );
+                    FileData::namecmp );
 
   set_difference(   old.begin(),     old.end(),
                     current.begin(), current.end(),
                     inserter( deleted, deleted.begin() ),
-                    cmp );
+                    FileData::namecmp );
 
   set_intersection( current.begin(), current.end(),
                     old.begin(),     old.end(),
                     inserter( common, common.begin() ),
-                    cmp );
+                    FileData::namecmp );
 
   set_intersection( old.begin(),    old.end(),
                     common.begin(), common.end(),
                     inserter( old_common, old_common.begin() ),
-                    cmp );
+                    FileData::namecmp );
 }
 
 template<class INT>
@@ -177,12 +175,12 @@ int main() {
   unsigned long long modified_blocks, modified_bytes;
   sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
 
-  // Now, sort the backups by filesize and build a list that'll fit on a DVD
+  // Now, sort the backups by filesize (decreasing) and build a list that'll fit
+  // on a DVD
   file_vector backups_s;
   copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
 
-  FileDataSizeCmp sizecmp;
-  sort( backups_s.begin(), backups_s.end(), sizecmp );
+  sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
 
   file_set final;
   unsigned long long space = 0x100000000ULL;
@@ -191,23 +189,26 @@ int main() {
 
   // Copy files over until full or out of files
   bool complete
-    = copy_until_full( backups_s.rbegin(), backups_s.rend(), final_i, space );
+    = copy_until_full( backups_s.begin(), backups_s.end(), final_i, space );
 
   // Track the size filled up by essential backups
   unsigned long long essential_blocks, essential_bytes;
   sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
 
-  // Now, sort the non-backed-up list by last_backup_date and back-fill
+  // Now, sort the non-backed-up list by last_backup_date, then by filesize
+  // (decreasing) and back-fill.  This should minimize the number of DVDs in the
+  // collection left with actual content.
   if( 0 != space ) {
     file_vector leftovers;
-    FileDataNameCmp cmp;
     set_difference( current.begin(), current.end(),
                     final.begin(),   final.end(),
                     back_inserter( leftovers ),
-                    cmp );
+                    FileData::namecmp );
 
-    FileDataLastBackupCmp lastbackupcmp;
-    sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
+    // Achieve 'last backup date then by filesize' by first sorting by filesize
+    // and then running stable sort by last backup date.
+    sort(        leftovers.rbegin(), leftovers.rend(), FileData::sizecmp );
+    stable_sort( leftovers.begin(),  leftovers.end(),  FileData::lastbackupcmp );
 
     copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
   }
index 0870b0d87b70a69296bfda06f6993b091eb603bf..5d030bf5f46918d09b5975e2abe8eaf844600531 100755 (executable)
@@ -42,9 +42,12 @@ fi
 renice 0 $$
 $cronstopstart start
 
+# I don't know if this *really* helps but give cdrecord a chance to clean up.
+sleep 60
+
 # Now verify the disk by running md5sum on the entire contents of the disk
 md5sum=$(tempfile)
-dd if=$dev bs=1M count=4440 2>$logfile | md5sum | awk '{print$1}' > $md5sum
+dd if=$dev bs=1M count=4440 2>>$logfile | md5sum | awk '{print$1}' > $md5sum
 
 # Check that the md5sums match
 if ! cmp $md5sum $img.md5sum; then