Regular commit
[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 current_time() {
16   unsigned long long rc = 0;
17   time_t now_tt = time( 0 );
18   tm *now = localtime( &now_tt );
19   rc += ( now->tm_year + 1900ULL ) * 10000000000ULL;
20   rc += ( now->tm_mon  + 1ULL )    * 100000000ULL;
21   rc +=   now->tm_mday             * 1000000ULL;
22   rc +=   now->tm_hour             * 10000ULL;
23   rc +=   now->tm_min              * 100ULL;
24   rc +=   now->tm_sec;
25
26   return rc;
27 }
28
29 template<class I, class O, class INT>
30 bool copy_until_full( I begin, I end, O out, INT &space ) {
31   bool complete = true;
32
33   I i = begin;
34   while( 0 != space && i != end ) {
35     INT size = (*i)->getFileSize();
36     INT blocksize = blocks( size ) * bytes_in_block;
37
38     if( blocksize <= space ) {
39       space -= blocksize;
40       out = *i;
41       ++out;
42     } else {
43       // We missed a file that should be included so the backup is not complete
44       complete = false;
45     }
46     ++i;
47   }
48   return complete;
49 }
50
51 template<class SET>
52 void populate_set( istream &in, SET &files ) {
53   do {
54     FileData *data = new FileData();
55     in >> data;
56     if( data->getFileName().size() ) {
57       files.insert( data );
58     } else {
59       delete data;
60     }
61   } while( ! in.eof() );
62 }
63
64 template<class SET>
65 void partition_sets( const SET &current, const SET &old,
66                      SET &added, SET &common, SET &old_common, SET &deleted  ) {
67   FileDataNameCmp cmp;
68
69   set_difference( current.begin(), current.end(),
70                   old.begin(),     old.end(),
71                   inserter( added, added.begin() ),
72                   cmp );
73
74   set_difference( old.begin(),     old.end(),
75                   current.begin(), current.end(),
76                   inserter( deleted, deleted.begin() ),
77                   cmp );
78
79   set_union(      current.begin(), current.end(),
80                   old.begin(),     old.end(),
81                   inserter( common, common.begin() ),
82                   cmp );
83
84   set_union(      old.begin(),    old.end(),
85                   common.begin(), common.end(),
86                   inserter( old_common, old_common.begin() ),
87                   cmp );
88 }
89
90 template<class INT>
91 INT blocks( const INT &bytes ) {
92   INT numblocks = bytes / bytes_in_block;
93   if( 0 != bytes % bytes_in_block ) numblocks++;
94
95   return numblocks;
96 }
97
98 template<class ITER, class INT>
99 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
100   numblocks = 0;
101   numbytes  = 0;
102
103   while( begin != end ) {
104     INT filesize = (*begin)->getFileSize();
105
106     numbytes  += filesize;
107     numblocks += blocks( filesize );
108     begin++;
109   }
110 }
111
112 template<class I, class O>
113 void copy_filenames( I begin, const I &end, O out ) {
114   while( begin != end ) {
115     string output = (*begin)->getFileName();
116     output.push_back( 0 );
117     *out = output;
118     ++out;
119     ++begin;
120   }
121 }
122
123 template<class ITER>
124 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
125   while( begin != end ) {
126     (*begin)->setLastBackupDate( date );
127     ++begin;
128   }
129 }
130
131 template<class ITER>
132 void delete_objects( ITER begin, const ITER &end ) {
133   while( begin != end ) {
134     delete *begin;
135     ++begin;
136   }
137 }
138
139 int main() {
140   // Parse the list of current files on stdin
141   file_set current;
142   populate_set( cin, current );
143
144   file_set backed_up;
145   ifstream db( dbname );
146   if( db && db.good() ) {
147     populate_set( db, backed_up );
148   }
149
150   // Now divide the two sets into three sets (added, deleted and common )
151   file_set added, deleted, common, old_common;
152   partition_sets( current, backed_up, added, common, old_common, deleted );
153
154   // Now find the list of files to backup.
155   file_set backups;
156   insert_iterator<file_set> backups_i( backups, backups.begin() );
157
158   // backup all added files
159   copy( added.begin(), added.end(), backups_i );
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_files;
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_files.push_back( *i );
172   }
173
174   copy( modified_files.begin(), modified_files.end(), backups_i );
175
176   // Track the total size of modified files
177   unsigned long long modified_blocks, modified_bytes;
178   sizes( modified_files.begin(), modified_files.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; // 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
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   unsigned long long now = current_time();
220   updateLastBackupDate( final.begin(), final.end(), now );
221
222   // Write the 'current' list to the dbfile
223   ofstream dbout( dbname );
224   copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
225
226   // Write the 'final' list to stdout
227   copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
228
229   cerr << now << endl << endl;
230
231   cerr << "Need backing up..." << endl;
232   cerr << "     Added Bytes:            " << added_bytes << endl;
233   cerr << "     Added Blocks:           " << added_blocks << endl;
234   cerr << "     Modified Bytes:         " << modified_bytes << endl;
235   cerr << "     Modified Blocks:        " << modified_blocks << endl << endl;
236
237   cerr << "Will be backed up..." << endl;
238   cerr << "     Essential Bytes:        " << essential_bytes << endl;
239   cerr << "     Essential Blocks:       " << essential_blocks << endl;
240   cerr << "     Total Bytes:            " << total_bytes << endl;
241   cerr << "     Total Blocks:           " << total_blocks << endl << endl;
242
243   if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
244
245   // Clean-up
246   delete_objects( backed_up.begin(), backed_up.end() );
247   delete_objects( current.begin(),   current.end() );
248 }