2014-12-11 10:50:15 +03:00
|
|
|
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
|
2014-11-27 19:20:41 +03:00
|
|
|
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
|
|
|
|
|
2014-12-15 14:13:34 +03:00
|
|
|
#include "backup_collector.hh"
|
2014-11-27 19:20:41 +03:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "bundle.hh"
|
|
|
|
#include "chunk_index.hh"
|
|
|
|
#include "backup_restorer.hh"
|
|
|
|
#include "backup_file.hh"
|
2015-01-25 19:33:01 +03:00
|
|
|
#include "backup_exchanger.hh"
|
2014-11-27 19:20:41 +03:00
|
|
|
|
|
|
|
#include "debug.hh"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
2015-02-04 14:38:22 +03:00
|
|
|
void BundleCollector::startIndex( string const & indexFn )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
indexModified = false;
|
|
|
|
indexTotalChunks = indexUsedChunks = 0;
|
|
|
|
indexModifiedBundles = indexKeptBundles = indexRemovedBundles = 0;
|
|
|
|
}
|
2014-11-27 19:20:41 +03:00
|
|
|
|
2015-02-04 14:38:22 +03:00
|
|
|
void BundleCollector::finishIndex( string const & indexFn )
|
|
|
|
{
|
|
|
|
if ( indexModified )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
verbosePrintf( "Chunks: %d used / %d total, bundles: %d kept / %d modified / %d removed\n",
|
|
|
|
indexUsedChunks, indexTotalChunks, indexKeptBundles, indexModifiedBundles, indexRemovedBundles);
|
|
|
|
filesToUnlink.push_back( indexFn );
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
2015-02-04 14:38:22 +03:00
|
|
|
}
|
2014-11-27 19:20:41 +03:00
|
|
|
|
2015-02-04 14:38:22 +03:00
|
|
|
void BundleCollector::startBundle( Bundle::Id const & bundleId )
|
|
|
|
{
|
|
|
|
savedId = bundleId;
|
|
|
|
totalChunks = 0;
|
|
|
|
usedChunks = 0;
|
|
|
|
}
|
2014-11-27 19:20:41 +03:00
|
|
|
|
2015-02-04 14:38:22 +03:00
|
|
|
void BundleCollector::processChunk( ChunkId const & chunkId )
|
|
|
|
{
|
|
|
|
totalChunks++;
|
|
|
|
if ( usedChunkSet.find( chunkId ) != usedChunkSet.end() )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
usedChunks++;
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
2015-02-04 14:38:22 +03:00
|
|
|
}
|
2014-11-27 19:20:41 +03:00
|
|
|
|
2015-02-04 14:38:22 +03:00
|
|
|
void BundleCollector::finishBundle( Bundle::Id const & bundleId, BundleInfo const & info )
|
|
|
|
{
|
|
|
|
string i = Bundle::generateFileName( savedId, "", false );
|
|
|
|
indexTotalChunks += totalChunks;
|
|
|
|
indexUsedChunks += usedChunks;
|
|
|
|
if ( usedChunks == 0 )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
if ( verbose )
|
|
|
|
printf( "delete %s\n", i.c_str() );
|
|
|
|
filesToUnlink.push_back( Dir::addPath( bundlesPath, i ) );
|
|
|
|
indexModified = true;
|
|
|
|
indexRemovedBundles++;
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
2015-02-04 14:38:22 +03:00
|
|
|
else if ( usedChunks < totalChunks )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
if ( verbose )
|
|
|
|
printf( "%s: used %d/%d\n", i.c_str(), usedChunks, totalChunks );
|
|
|
|
filesToUnlink.push_back( Dir::addPath( bundlesPath, i ) );
|
|
|
|
indexModified = true;
|
|
|
|
// Copy used chunks to the new index
|
|
|
|
string chunk;
|
|
|
|
size_t chunkSize;
|
|
|
|
for ( int x = info.chunk_record_size(); x--; )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
BundleInfo_ChunkRecord const & record = info.chunk_record( x );
|
|
|
|
ChunkId id( record.id() );
|
|
|
|
if ( usedChunkSet.find( id ) != usedChunkSet.end() )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
chunkStorageReader->get( id, chunk, chunkSize );
|
|
|
|
chunkStorageWriter->add( id, chunk.data(), chunkSize );
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-04 14:38:22 +03:00
|
|
|
indexModifiedBundles++;
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
2015-02-04 14:38:22 +03:00
|
|
|
else
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
chunkStorageWriter->addBundle( info, savedId );
|
|
|
|
if ( verbose )
|
|
|
|
printf( "keep %s\n", i.c_str() );
|
|
|
|
indexKeptBundles++;
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 14:38:22 +03:00
|
|
|
void BundleCollector::commit()
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
for ( int i = filesToUnlink.size(); i--; )
|
2014-11-27 19:20:41 +03:00
|
|
|
{
|
2015-02-04 14:38:22 +03:00
|
|
|
unlink( filesToUnlink[i].c_str() );
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|
2015-02-04 14:38:22 +03:00
|
|
|
filesToUnlink.clear();
|
|
|
|
chunkStorageWriter->commit();
|
2014-11-27 19:20:41 +03:00
|
|
|
}
|