8 #include "filedata.hpp"
12 static const unsigned int bytes_in_block = 0x800;
13 static const char * dbname = "/var/lib/backups/backups.db";
15 unsigned long long read_time( istream &i ) {
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 );
23 if( char_traits<char>::eof() == c ) { i.setstate( ios_base::eofbit ); }
25 return atoll( date_string.c_str() );
28 template<class I, class O, class INT>
29 bool copy_until_full( I begin, I end, O out, INT &space ) {
33 while( 0 != space && i != end ) {
34 INT size = (*i)->getFileSize();
35 INT blocksize = blocks( size ) * bytes_in_block;
37 if( blocksize <= space ) {
42 // We missed a file that should be included so the backup is not complete
51 void populate_set( istream &in, SET &files ) {
53 FileData *data = new FileData();
55 if( data->getFileName().size() ) {
60 } while( ! in.eof() );
64 void partition_sets( const SET ¤t, const SET &old,
65 SET &added, SET &common, SET &old_common, SET &deleted ) {
68 set_difference( current.begin(), current.end(),
69 old.begin(), old.end(),
70 inserter( added, added.begin() ),
73 set_difference( old.begin(), old.end(),
74 current.begin(), current.end(),
75 inserter( deleted, deleted.begin() ),
78 set_union( current.begin(), current.end(),
79 old.begin(), old.end(),
80 inserter( common, common.begin() ),
83 set_union( old.begin(), old.end(),
84 common.begin(), common.end(),
85 inserter( old_common, old_common.begin() ),
90 INT blocks( const INT &bytes ) {
91 INT numblocks = bytes / bytes_in_block;
92 if( 0 != bytes % bytes_in_block ) numblocks++;
97 template<class ITER, class INT>
98 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
102 while( begin != end ) {
103 INT filesize = (*begin)->getFileSize();
105 numbytes += filesize;
106 numblocks += blocks( filesize );
111 template<class I, class O>
112 void copy_filenames( I begin, const I &end, O out ) {
113 while( begin != end ) {
114 string output = (*begin)->getFileName();
115 output.push_back( 0 );
123 void updateLastBackupDate( ITER begin, const ITER &end, unsigned long long date ) {
124 while( begin != end ) {
125 (*begin)->setLastBackupDate( date );
131 void delete_objects( ITER begin, const ITER &end ) {
132 while( begin != end ) {
139 // Get the date on stdin
140 unsigned long long now = read_time( cin );
142 // Parse the list of current files on stdin
144 populate_set( cin, current );
147 ifstream db( dbname );
148 if( db && db.good() ) {
149 populate_set( db, backed_up );
152 // Now divide the two sets into three sets (added, deleted and common )
153 file_set added, deleted, common, old_common;
154 partition_sets( current, backed_up, added, common, old_common, deleted );
156 // Now find the list of files to backup.
158 insert_iterator<file_set> backups_i( backups, backups.begin() );
160 // backup all added files
161 copy( added.begin(), added.end(), backups_i );
163 // Track the total size of added files
164 unsigned long long added_blocks, added_bytes;
165 sizes( added.begin(), added.end(), added_blocks, added_bytes );
167 file_vector modified_files;
168 // Backup files that have been modified
169 file_set::iterator i = common.begin(), j = old_common.begin();
170 for( ; i != common.end(); ++i, ++j ) {
171 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
173 if( needs_backup( *j, *i ) ) modified_files.push_back( *i );
176 copy( modified_files.begin(), modified_files.end(), backups_i );
178 // Track the total size of modified files
179 unsigned long long modified_blocks, modified_bytes;
180 sizes( modified_files.begin(), modified_files.end(), modified_blocks, modified_bytes );
182 // Now, sort the backups by filesize and build a list that'll fit on a DVD
183 file_vector backups_s;
184 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
186 FileDataSizeCmp sizecmp;
187 sort( backups_s.begin(), backups_s.end(), sizecmp );
190 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
192 insert_iterator<file_set> final_i( final, final.begin() );
194 // Copy files over until full or out of files
196 = copy_until_full( backups_s.rbegin(), backups_s.rend(), final_i, space );
198 // Track the size filled up by essential backups
199 unsigned long long essential_blocks, essential_bytes;
200 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
202 // Now, sort the non-backed-up list by last_backup_date and back-fill
204 file_vector leftovers;
206 set_difference( current.begin(), current.end(),
207 final.begin(), final.end(),
208 back_inserter( leftovers ),
211 FileDataLastBackupCmp lastbackupcmp;
212 sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
214 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
217 // Track the total size to be copied to the dvd
218 unsigned long long total_blocks, total_bytes;
219 sizes( final.begin(), final.end(), total_blocks, total_bytes );
221 updateLastBackupDate( final.begin(), final.end(), now );
223 // Write the 'current' list to the dbfile
224 ofstream dbout( dbname );
225 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout ) );
227 // Write the 'final' list to stdout
228 copy_filenames( final.begin(), final.end(), ostream_iterator<string>( cout ) );
230 cerr << now << endl << endl;
232 cerr << "Need backing up..." << endl;
233 cerr << " Added Bytes: " << added_bytes << endl;
234 cerr << " Added Blocks: " << added_blocks << endl;
235 cerr << " Modified Bytes: " << modified_bytes << endl;
236 cerr << " Modified Blocks: " << modified_blocks << endl << endl;
238 cerr << "Will be backed up..." << endl;
239 cerr << " Essential Bytes: " << essential_bytes << endl;
240 cerr << " Essential Blocks: " << essential_blocks << endl;
241 cerr << " Total Bytes: " << total_bytes << endl;
242 cerr << " Total Blocks: " << total_blocks << endl << endl;
244 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
247 delete_objects( backed_up.begin(), backed_up.end() );
248 delete_objects( current.begin(), current.end() );