Bump the revision number
[backups/.git] / main.cpp
index a5518501772afaa7d180a0f07c2ff3b077991efe..80385fe57651b2098dd9b8a70ba0117b6148b5d9 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -4,13 +4,15 @@
 #include <algorithm>
 #include <cassert>
 #include <ctime>
+#include <math.h>
 
 #include "filedata.hpp"
 
 using namespace std;
 
-static const unsigned int bytes_in_block = 0x800;
-static const char * dbname = "/var/lib/backups/backups.db";
+static unsigned int bytes_in_block;
+static const char * dbname_in = getenv("backupdbin");
+static const char * dbname_out = getenv("backupdbout");
 
 unsigned long long read_time( istream &i ) {
   string date_string;
@@ -29,20 +31,19 @@ template<class I, class O, class INT>
 bool copy_until_full( I begin, I end, O out, INT &space ) {
   bool complete = true;
 
-  I i = begin;
-  while( 0 != space && i != end ) {
-    INT size = (*i)->getFileSize();
+  while( begin != end ) {
+    INT size = (*begin)->getFileSize();
     INT blocksize = blocks( size ) * bytes_in_block;
 
     if( blocksize <= space ) {
       space -= blocksize;
-      out = *i;
+      *out = *begin;
       ++out;
     } else {
       // We missed a file that should be included so the backup is not complete
       complete = false;
     }
-    ++i;
+    ++begin;
   }
   return complete;
 }
@@ -63,27 +64,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 );
-
-  set_difference( old.begin(),     old.end(),
-                  current.begin(), current.end(),
-                  inserter( deleted, deleted.begin() ),
-                  cmp );
-
-  set_union(      current.begin(), current.end(),
-                  old.begin(),     old.end(),
-                  inserter( common, common.begin() ),
-                  cmp );
-
-  set_union(      old.begin(),    old.end(),
-                  common.begin(), common.end(),
-                  inserter( old_common, old_common.begin() ),
-                  cmp );
+  set_difference(   current.begin(), current.end(),
+                    old.begin(),     old.end(),
+                    inserter( added, added.begin() ),
+                    FileData::namecmp );
+
+  set_difference(   old.begin(),     old.end(),
+                    current.begin(), current.end(),
+                    inserter( deleted, deleted.begin() ),
+                    FileData::namecmp );
+
+  set_intersection( current.begin(), current.end(),
+                    old.begin(),     old.end(),
+                    inserter( common, common.begin() ),
+                    FileData::namecmp );
+
+  set_intersection( old.begin(),    old.end(),
+                    common.begin(), common.end(),
+                    inserter( old_common, old_common.begin() ),
+                    FileData::namecmp );
 }
 
 template<class INT>
@@ -104,7 +103,7 @@ void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
 
     numbytes  += filesize;
     numblocks += blocks( filesize );
-    begin++;
+    ++begin;
   }
 }
 
@@ -136,6 +135,20 @@ void delete_objects( ITER begin, const ITER &end ) {
 }
 
 int main() {
+
+  // Check to make sure required env variables are set
+  if( getenv( "backupdbin"  ) == NULL ||
+      getenv( "backupdbout" ) == NULL ||
+      getenv( "blocksize"   ) == NULL ||
+      getenv( "availsizemb" ) == NULL )
+  {
+     cerr << "Required environment variables are not set. Exiting." << endl;
+     return 1;
+  }
+
+  // Setup our bytes_in_block value
+  bytes_in_block = atoll(getenv("blocksize"));
+
   // Get the date on stdin
   unsigned long long now = read_time( cin );
 
@@ -144,7 +157,7 @@ int main() {
   populate_set( cin, current );
 
   file_set backed_up;
-  ifstream db( dbname );
+  ifstream db( dbname_in );
   if( db && db.good() ) {
     populate_set( db, backed_up );
   }
@@ -155,61 +168,64 @@ int main() {
 
   // Now find the list of files to backup.
   file_set backups;
-  insert_iterator<file_set> backups_i( backups, backups.begin() );
 
   // backup all added files
-  copy( added.begin(), added.end(), backups_i );
+  copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
 
   // Track the total size of added files
   unsigned long long added_blocks, added_bytes;
   sizes( added.begin(), added.end(), added_blocks, added_bytes );
 
-  file_vector modified_files;
+  file_vector modified_v;
   // Backup files that have been modified
   file_set::iterator i = common.begin(), j = old_common.begin();
   for( ; i != common.end(); ++i, ++j ) {
     (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
 
-    if( needs_backup( *j, *i ) ) modified_files.push_back( *i );
+    if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
   }
 
-  copy( modified_files.begin(), modified_files.end(), backups_i );
+  copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
 
   // Track the total size of modified files
   unsigned long long modified_blocks, modified_bytes;
-  sizes( modified_files.begin(), modified_files.end(), 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; // After looking at how big the ISO can be and how many 1K blocks are left after formatting the filesystem I decided on an even 4GB target
+  const unsigned long long availsizemb = atoll( getenv("availsizemb") ) * 0x100000ull;
+  unsigned long long space = availsizemb;
 
   insert_iterator<file_set> final_i( final, final.begin() );
 
   // 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 );
   }
@@ -218,10 +234,17 @@ int main() {
   unsigned long long total_blocks, total_bytes;
   sizes( final.begin(), final.end(), total_blocks, total_bytes );
 
+  // Track how many disks there are remaining to be burned
+  unsigned long long disks_remaining = 0;
+  if(modified_bytes || added_bytes)
+    disks_remaining = static_cast<unsigned long long>(
+        ceil( static_cast<double>( modified_bytes + added_bytes ) / availsizemb ) - 1
+        );
+
   updateLastBackupDate( final.begin(), final.end(), now );
 
   // Write the 'current' list to the dbfile
-  ofstream dbout( dbname );
+  ofstream dbout( dbname_out );
   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
 
   // Write the 'final' list to stdout
@@ -230,16 +253,17 @@ int main() {
   cerr << now << endl << endl;
 
   cerr << "Need backing up..." << endl;
-  cerr << "    Added Bytes:            " << added_bytes << endl;
-  cerr << "    Added Blocks:           " << added_blocks << endl;
-  cerr << "    Modified Bytes:         " << modified_bytes << endl;
-  cerr << "    Modified Blocks:        " << modified_blocks << endl << endl;
+  cerr << "     Added Bytes:            " << added_bytes << endl;
+  cerr << "     Added Blocks:           " << added_blocks << endl;
+  cerr << "     Modified Bytes:         " << modified_bytes << endl;
+  cerr << "     Modified Blocks:        " << modified_blocks << endl;
+  cerr << "     Disks Remaining:        " << disks_remaining << endl << endl;
 
   cerr << "Will be backed up..." << endl;
-  cerr << "    Essential Bytes:        " << essential_bytes << endl;
-  cerr << "    Essential Blocks:       " << essential_blocks << endl;
-  cerr << "    Total Bytes:            " << total_bytes << endl;
-  cerr << "    Total Blocks:           " << total_blocks << endl << endl;
+  cerr << "     Essential Bytes:        " << essential_bytes << endl;
+  cerr << "     Essential Blocks:       " << essential_blocks << endl;
+  cerr << "     Total Bytes:            " << total_bytes << endl;
+  cerr << "     Total Blocks:           " << total_blocks << endl << endl;
 
   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }