Merge branch 'master' of ecbaldwin.net:/home/cnb/dvl/backup/
[backups/.git] / main.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <iterator>
4 #include <algorithm>
5 #include <cassert>
6 #include <ctime>
7
8 #include "filedata.hpp"
9
10 using namespace std;
11
12 static const unsigned int bytes_in_block = 0x800;
13 static const char * dbname = "/var/lib/backups/backups.db";
14
15 unsigned long long read_time( istream &i ) {
16   string date_string;
17
18   int c;
19   // Todo, don't use char_traits<char> directly here
20   for( c = i.get(); 0 != c && char_traits<char>::eof() != c; c = i.get() ) {
21     date_string.push_back( c );
22   }
23   if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
24
25   return atoll( date_string.c_str() );
26 }
27
28 template<class I, class O, class INT>
29 bool copy_until_full( I begin, I end, O out, INT &space ) {
30   bool complete = true;
31
32   while( 0 != space && begin != end ) {
33     INT size = (*begin)->getFileSize();
34     INT blocksize = blocks( size ) * bytes_in_block;
35
36     if( blocksize <= space ) {
37       space -= blocksize;
38       out = *begin;
39       ++out;
40     } else {
41       // We missed a file that should be included so the backup is not complete
42       complete = false;
43     }
44     ++begin;
45   }
46   return complete;
47 }
48
49 template<class SET>
50 void populate_set( istream &in, SET &files ) {
51   do {
52     FileData *data = new FileData();
53     in >> data;
54     if( data->getFileName().size() ) {
55       files.insert( data );
56     } else {
57       delete data;
58     }
59   } while( ! in.eof() );
60 }
61
62 template<class SET>
63 void partition_sets( const SET &current, const SET &old,
64                      SET &added, SET &common, SET &old_common, SET &deleted  ) {
65   FileDataNameCmp cmp;
66
67   set_difference(   current.begin(), current.end(),
68                     old.begin(),     old.end(),
69                     inserter( added, added.begin() ),
70                     cmp );
71
72   set_difference(   old.begin(),     old.end(),
73                     current.begin(), current.end(),
74                     inserter( deleted, deleted.begin() ),
75                     cmp );
76
77   set_intersection( current.begin(), current.end(),
78                     old.begin(),     old.end(),
79                     inserter( common, common.begin() ),
80                     cmp );
81
82   set_intersection( old.begin(),    old.end(),
83                     common.begin(), common.end(),
84                     inserter( old_common, old_common.begin() ),
85                     cmp );
86 }
87
88 template<class INT>
89 INT blocks( const INT &bytes ) {
90   INT numblocks = bytes / bytes_in_block;
91   if( 0 != bytes % bytes_in_block ) numblocks++;
92
93   return numblocks;
94 }
95
96 template<class ITER, class INT>
97 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
98   numblocks = 0;
99   numbytes  = 0;
100
101   while( begin != end ) {
102     INT filesize = (*begin)->getFileSize();
103
104     numbytes  += filesize;
105     numblocks += blocks( filesize );
106     ++begin;
107   }
108 }
109
110 template<class I, class O>
111 void copy_filenames( I begin, const I &end, O out ) {
112   while( begin != end ) {
113     string output = (*begin)->getFileName();
114     output.push_back( 0 );
115     *out = output;
116     ++out;
117     ++begin;
118   }
119 }
120
121 template<class ITER>
122 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
123   while( begin != end ) {
124     (*begin)->setLastBackupDate( date );
125     ++begin;
126   }
127 }
128
129 template<class ITER>
130 void delete_objects( ITER begin, const ITER &end ) {
131   while( begin != end ) {
132     delete *begin;
133     ++begin;
134   }
135 }
136
137 int main() {
138   // Get the date on stdin
139   unsigned long long now = read_time( cin );
140
141   // Parse the list of current files on stdin
142   file_set current;
143   populate_set( cin, current );
144
145   file_set backed_up;
146   ifstream db( dbname );
147   if( db && db.good() ) {
148     populate_set( db, backed_up );
149   }
150
151   // Now divide the two sets into three sets (added, deleted and common )
152   file_set added, deleted, common, old_common;
153   partition_sets( current, backed_up, added, common, old_common, deleted );
154
155   // Now find the list of files to backup.
156   file_set backups;
157
158   // backup all added files
159   copy( added.begin(), added.end(), inserter( backups, backups.begin() ) );
160
161   // Track the total size of added files
162   unsigned long long added_blocks, added_bytes;
163   sizes( added.begin(), added.end(), added_blocks, added_bytes );
164
165   file_vector modified_v;
166   // Backup files that have been modified
167   file_set::iterator i = common.begin(), j = old_common.begin();
168   for( ; i != common.end(); ++i, ++j ) {
169     (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
170
171     if( needs_backup( *j, *i ) ) modified_v.push_back( *i );
172   }
173
174   copy( modified_v.begin(), modified_v.end(), inserter( backups, backups.begin() ) );
175
176   // Track the total size of modified files
177   unsigned long long modified_blocks, modified_bytes;
178   sizes( modified_v.begin(), modified_v.end(), modified_blocks, modified_bytes );
179
180   // Now, sort the backups by filesize and build a list that'll fit on a DVD
181   file_vector backups_s;
182   copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
183
184   FileDataSizeCmp sizecmp;
185   sort( backups_s.begin(), backups_s.end(), sizecmp );
186
187   file_set final;
188   unsigned long long space = 0x100000000ULL;
189
190   insert_iterator<file_set> final_i( final, final.begin() );
191
192   // Copy files over until full or out of files
193   bool complete
194     = copy_until_full( backups_s.rbegin(), backups_s.rend(), final_i, space );
195
196   // Track the size filled up by essential backups
197   unsigned long long essential_blocks, essential_bytes;
198   sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
199
200   // Now, sort the non-backed-up list by last_backup_date and back-fill
201   if( 0 != space ) {
202     file_vector leftovers;
203     FileDataNameCmp cmp;
204     set_difference( current.begin(), current.end(),
205                     final.begin(),   final.end(),
206                     back_inserter( leftovers ),
207                     cmp );
208
209     FileDataLastBackupCmp lastbackupcmp;
210     sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
211
212     copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
213   }
214
215   // Track the total size to be copied to the dvd
216   unsigned long long total_blocks, total_bytes;
217   sizes( final.begin(), final.end(), total_blocks, total_bytes );
218
219   updateLastBackupDate( final.begin(), final.end(), now );
220
221   // Write the 'current' list to the dbfile
222   ofstream dbout( dbname );
223   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
224
225   // Write the 'final' list to stdout
226   copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
227
228   cerr << now << endl << endl;
229
230   cerr << "Need backing up..." << endl;
231   cerr << "     Added Bytes:            " << added_bytes << endl;
232   cerr << "     Added Blocks:           " << added_blocks << endl;
233   cerr << "     Modified Bytes:         " << modified_bytes << endl;
234   cerr << "     Modified Blocks:        " << modified_blocks << endl << endl;
235
236   cerr << "Will be backed up..." << endl;
237   cerr << "     Essential Bytes:        " << essential_bytes << endl;
238   cerr << "     Essential Blocks:       " << essential_blocks << endl;
239   cerr << "     Total Bytes:            " << total_bytes << endl;
240   cerr << "     Total Blocks:           " << total_blocks << endl << endl;
241
242   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
243
244   // Clean-up
245   delete_objects( backed_up.begin(), backed_up.end() );
246   delete_objects( current.begin(),   current.end() );
247 }