using namespace std;
-static const unsigned int bytes_in_block = 0x800;
-static const char * dbname = "/var/lib/backups/backups.db.tmp";
+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;
}
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 );
populate_set( cin, current );
file_set backed_up;
- ifstream db( dbname );
+ ifstream db( dbname_in );
if( db && db.good() ) {
populate_set( db, backed_up );
}
sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp );
file_set final;
- unsigned long long space = 0x100000000ULL;
-
+ unsigned long long space = atoll(getenv("availsizemb")) * 1048576ull;
+
insert_iterator<file_set> final_i( final, final.begin() );
// Copy files over until full or out of files
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 = (unsigned long long)ceil(static_cast<double>(modified_bytes+added_bytes)/(atoll(getenv("availsizemb"))*1048576ull))-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
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 << " Modified Blocks: " << modified_blocks << endl;
+ cerr << " Disks Remaining: " << disks_remaining << endl << endl;
cerr << "Will be backed up..." << endl;
cerr << " Essential Bytes: " << essential_bytes << endl;
# If the noburn file is there then don't burn.
[ -f "$noburnfile" ] && exit 0
-# Discover disk images by looking for .img.md5sum files in $imagedir
-imgmd5=$(ls $imagedir/*.img.md5sum 2>/dev/null | head -n 1)
+# Discover disk images by looking for .iso.md5sum files in $imagedir
+imgmd5=$(ls $imagedir/*.iso.md5sum 2>/dev/null | head -n 1)
# If we didn't find an non-empty file then exit gracefully
[ -z "$imgmd5" ] && exit 0
tmpdir="/dev/shm/backups"
imagedir="/backup/imgs"
currentfiles="$tmpdir/files.db"
-backupdb="$datadir/backups.db"
backups="$tmpdir/backup-list.db"
statusfile="$imagedir/backup-status.txt"
lastbackupfile="$imagedir/lastbackup"
sdev="ATA:1,0,0"
noburnfile="$imagedir/noburn"
+# These are exported so lsbackups can have access to them
+export backupdbin="$datadir/backups.db"
+export backupdbout="$datadir/backups.db.tmp"
+export imagesizemb=4440
+export availsizemb=4096
+export blocksize=2048
+
backupdirs="
/etc
/home
# source the system specific configuration
[ -f /etc/lsbackups.conf ] && . /etc/lsbackups.conf
+# process command line arguments
+case "$1" in
+ -s) simulate=1;;
+esac
+
# file locations and other values
today=$(date +%Y%m%d%H%M%S)
-isoimage="$imagedir/$today.img"
+isoimage="$imagedir/$today.iso"
isomountdir="$isoimage.mnt"
restorescript="$scriptsdir/restore.sh"
fi
echo "-> Creating the iso image in $isoimage"
-dd of=$isoimage bs=1M count=0 seek=4440
+dd of=$isoimage bs=1M count=0 seek=$imagesizemb
[ $? != 0 ] && err "dd failed to create $isoimage"
-mke2fs -b 2048 -F $isoimage
+mke2fs -b $blocksize -F $isoimage
if [ $? != 0 ]
then
err "Unable to create the iso image: $isoimage"
fi
+e2label $isoimage $today
+
+if [ $? != 0 ]
+then
+ rm -rf $isomountdir $isoimage
+ err "Unable to label the iso image: $isoimage"
+fi
+
echo "-> Mounting the iso image"
mount -t ext2 -o loop $isoimage $isomountdir
if [ $? != 0 ]
{
for type in d f l; do
findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
- if [ -z $excludedirs ]
+ if [ -z "$excludedirs" ]
then
find $backupdirs -type $type -printf "$findformat"
else
find $backupdirs -type $type -printf "$findformat" | grep -z -v -f $tmpdir/excluded
fi
done
-} > $currentfiles
+} > $currentfiles
+
+if [ ! -z $simulate ]
+then
+ backupdbin=$backupdbout
+fi
echo "-> Determining list of files to backup with lsbackups"
{
cat $currentfiles
} | lsbackups > $backups 2>$statusfile
+if [ ! -z $simulate ]
+then
+ umount $isomountdir
+ rm -rf $isomountdir $isoimage
+ cat $statusfile
+ echo "-> Simulated backup complete! Backup database: $backupdbout"
+ exit;
+fi
+
echo "-> Running rsync to pack the image"
rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
cat $backups | rsync $rsyncopts / $isomountdir
if [ $? != 0 ]
then
umount $isomountdir
- rm -rf $isomountdir $isoimage $backupdb.tmp
+ rm -rf $isomountdir $isoimage $backupdbout
err "Unable to rsync to pack the image"
fi
echo "-> Copying over database and status file"
-mv $backupdb.tmp $backupdb
-cp $statusfile $backupdb $restorescript $isomountdir
+cp $backupdbout $isomountdir/$(basename $backupdbin)
+[ $? != 0 ] && err "Unable to copy the database to $isomountdir"
-[ $? != 0 ] && err "Unable to copy the database and status file"
+cp $statusfile $restorescript $isomountdir
+[ $? != 0 ] && err "Unable to copy the status file to $isomountdir"
echo "-> Unmounting image"
umount $isomountdir
echo $today > $lastbackupfile
+echo "-> Moving new database to $backupdbin"
+mv $backupdbout $backupdbin
+[ $? != 0 ] && err "Unable to move backupdb: $backupdbout to $backupdbin"
+