Bump the revision number
[backups/.git] / main.cpp
index 06d1a2f9ecf0847e73e47d83dc0c17df4c2f17f8..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,13 +31,13 @@ template<class I, class O, class INT>
 bool copy_until_full( I begin, I end, O out, INT &space ) {
   bool complete = true;
 
-  while( 0 != space && begin != end ) {
+  while( begin != end ) {
     INT size = (*begin)->getFileSize();
     INT blocksize = blocks( size ) * bytes_in_block;
 
     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
@@ -133,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 );
 
@@ -141,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 );
   }
@@ -183,7 +199,8 @@ int main() {
   sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
 
   file_set final;
-  unsigned long long space = 0x100000000ULL;
+  const unsigned long long availsizemb = atoll( getenv("availsizemb") ) * 0x100000ull;
+  unsigned long long space = availsizemb;
 
   insert_iterator<file_set> final_i( final, final.begin() );
 
@@ -205,7 +222,7 @@ int main() {
                     back_inserter( leftovers ),
                     FileData::namecmp );
 
-    // Achieve 'last back date then by filesize' by first sorting by filesizing
+    // 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 );
@@ -217,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
@@ -229,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; }