8 #include "filedata.hpp"
12 static const unsigned int bytes_in_block = 0x800;
13 static const char * dbname = "/var/tmp/backups.db";
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;
29 template<class I, class O, class INT>
30 bool copy_until_full( I begin, I end, O out, INT &space ) {
34 while( 0 != space && i != end ) {
35 INT size = (*i)->getFileSize();
36 INT blocksize = blocks( size ) * bytes_in_block;
38 if( blocksize <= space ) {
43 // We missed a file that should be included so the backup is not complete
52 void populate_set( istream &in, SET &files ) {
54 FileData *data = new FileData();
56 if( data->getFileName().size() ) {
61 } while( ! in.eof() );
65 void partition_sets( const SET ¤t, const SET &old,
66 SET &added, SET &common, SET &old_common, SET &deleted ) {
69 set_difference( current.begin(), current.end(),
70 old.begin(), old.end(),
71 inserter( added, added.begin() ),
74 set_difference( old.begin(), old.end(),
75 current.begin(), current.end(),
76 inserter( deleted, deleted.begin() ),
79 set_union( current.begin(), current.end(),
80 old.begin(), old.end(),
81 inserter( common, common.begin() ),
84 set_union( old.begin(), old.end(),
85 common.begin(), common.end(),
86 inserter( old_common, old_common.begin() ),
91 INT blocks( const INT &bytes ) {
92 INT numblocks = bytes / bytes_in_block;
93 if( 0 != bytes % bytes_in_block ) numblocks++;
98 template<class ITER, class INT>
99 void sizes( ITER begin, const ITER &end, INT &numblocks, INT &numbytes ) {
103 while( begin != end ) {
104 INT filesize = (*begin)->getFileSize();
106 numbytes += filesize;
107 numblocks += blocks( filesize );
113 // Parse the list of current files on stdin
115 populate_set( cin, current );
118 ifstream db( dbname );
119 if( db && db.good() ) {
120 populate_set( db, backed_up );
123 // Now divide the two sets into three sets (added, deleted and common )
124 file_set added, deleted, common, old_common;
125 partition_sets( current, backed_up, added, common, old_common, deleted );
127 // Now find the list of files to backup.
129 insert_iterator<file_set> backups_i( backups, backups.begin() );
131 // backup all added files
132 copy( added.begin(), added.end(), backups_i );
134 // Track the total size of added files
135 unsigned long long added_blocks, added_bytes;
136 sizes( backups.begin(), backups.end(), added_blocks, added_bytes );
138 file_vector modified_files;
139 // Backup files that have been modified
140 file_set::iterator i = common.begin(), j = old_common.begin();
141 for( ; i != common.end(); ++i, ++j ) {
142 (*i)->setLastBackupDate( (*j)->getLastBackupDate() );
144 if( needs_backup( *j, *i ) ) modified_files.push_back( *i );
147 copy( modified_files.begin(), modified_files.end(), backups_i );
149 // Track the total size of modified files
150 unsigned long long modified_blocks, modified_bytes;
151 sizes( modified_files.begin(), modified_files.end(), modified_blocks, modified_bytes );
153 // Now, sort the backups by filesize and build a list that'll fit on a DVD
154 file_vector backups_s;
155 copy( backups.begin(), backups.end(), back_inserter( backups_s ) );
157 FileDataSizeCmp sizecmp;
158 sort( backups_s.begin(), backups_s.end(), sizecmp );
161 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
163 insert_iterator<file_set> final_i( final, final.begin() );
165 // Copy files over until full or out of files
166 bool complete = copy_until_full( backups_s.rbegin(),
171 // Track the size filled up by essential backups
172 unsigned long long essential_blocks, essential_bytes;
173 sizes( final.begin(), final.end(), essential_blocks, essential_bytes );
175 // Now, sort the non-backed-up list by last_backup_date and back-fill
177 file_vector leftovers;
179 set_difference( current.begin(), current.end(),
180 final.begin(), final.end(),
181 back_inserter( leftovers ),
184 FileDataLastBackupCmp lastbackupcmp;
185 sort( leftovers.begin(), leftovers.end(), lastbackupcmp );
187 copy_until_full( leftovers.begin(), leftovers.end(), final_i, space );
190 // Track the total size to be copied to the dvd
191 unsigned long long total_blocks, total_bytes;
192 sizes( final.begin(), final.end(), total_blocks, total_bytes );
194 unsigned long long now = current_time();
195 for( file_set::iterator k = final.begin(); k != final.end(); ++k ) {
196 (*k)->setLastBackupDate( now );
199 // Write the 'current' list to the dbfile
200 ofstream dbout( dbname );
201 copy( current.begin(), current.end(), ostream_iterator<FileData*>( dbout, "" ) );
203 // Write the 'final' list to stdout
204 copy( final.begin(), final.end(), ostream_iterator<FileData*>( cout, "" ) );
206 cerr << "Need backing up..." << endl;
207 cerr << " Added Bytes: " << added_bytes << endl;
208 cerr << " Added Blocks: " << added_blocks << endl;
209 cerr << " Modified Bytes: " << modified_bytes << endl;
210 cerr << " Modified Blocks: " << modified_blocks << endl << endl;
212 cerr << "Will be backed up..." << endl;
213 cerr << " Essential Bytes: " << essential_bytes << endl;
214 cerr << " Essential Blocks: " << essential_blocks << endl;
215 cerr << " Total Bytes: " << total_bytes << endl;
216 cerr << " Total Blocks: " << total_blocks << endl << endl;
218 if( ! complete ) { cerr << "Backup is incomplete!" << endl; }
221 for( file_set::iterator i = backed_up.begin(); i != backed_up.end(); ++i ) { delete *i; }
222 for( file_set::iterator i = current.begin(); i != current.end(); ++i ) { delete *i; }