void RemoveOldRevisions(List <VolumeSnapshotRevision> revisionsThatCanBeRemoved)
 {
     foreach (VolumeSnapshotRevision revision in revisionsThatCanBeRemoved)
     {
         mDatabase.DeleteSnapshotRevision(revision);
         mArchive.DeleteRevision(revision);
     }
 }
        void MergeSnapshotRevisions(VolumeSnapshotDatabase database, VolumeSource source, BaseArchive archive, int numRevisionsToRemove)
        {
            try
            {
                mDatabase = database;
                mArchive  = archive;
                mBusy     = true;

                List <VolumeSnapshotRevision> revisionHistory = database.GetRevisionHistory();
                if (numRevisionsToRemove == revisionHistory.Count)
                {
                    // Need to remove all old revisions, delete everything
                    FileSync.__Log(this, "Merge deleting all revisions");
                    database.DeleteAllRevisions();
                }
                else
                {
                    // now we know how many revisions to remove, need to rebuild the new first revision.
                    VolumeSnapshotRevision currentRevision = revisionHistory[numRevisionsToRemove];
                    VolumeSnapshot         currentSnapshot = database.LoadSnapshotRevision(source, currentRevision);

                    FileSync.__Log(this, "Merge is turning revision [" + currentRevision.ToString() + "] into baseline");
                    TurnSnapshotIntoBaseline(currentSnapshot);

                    // Now go through all existing snapshot .xml files and change any files referencing
                    // a snapshot that has been removed and change it to the current snapshot revision.
                    for (int revisionNum = numRevisionsToRemove + 1; revisionNum < revisionHistory.Count; ++revisionNum)
                    {
                        VolumeSnapshotRevision incrementalRevision = revisionHistory[revisionNum];
                        VolumeSnapshot         incrementalSnapshot = database.LoadSnapshotRevision(source, incrementalRevision);

                        FileSync.__Log(this, "Merge is reflecting revision [" + incrementalRevision.ToString() + "] into new baseline [" + currentRevision.ToString() + "]");
                        UpdateSnapshotToReflectBaselineRevision(incrementalSnapshot, currentRevision);
                    }

                    // delete old revision data
                    for (int revisionNum = 0; revisionNum < numRevisionsToRemove; ++revisionNum)
                    {
                        VolumeSnapshotRevision revisionToDelete = revisionHistory[revisionNum];
                        FileSync.__Log(this, "Merge is deleting revision [" + revisionToDelete.ToString() + "]");
                        database.DeleteSnapshotRevision(revisionToDelete);
                    }
                }
            }
            catch (System.Exception ex)
            {
                FileSync.__LogError(this, "Volume.CheckForExpiredSnapshotRevisions", ex);
            }
            finally
            {
                mBusy = false;
            }
        }
예제 #3
0
        public bool DoBackup(ref bool outputToXmlRequired)
        {
            VolumeSnapshot currentSnapshot   = null;
            bool           backupSuccess     = false;
            bool           noChangesRequired = false;

            outputToXmlRequired = false;
            mCancelledByUser    = false;

            try
            {
                mBusy = true;

                FileSync.__Log(this, "Building standing on disk snapshot...");

                // Take a snapshot of the volume as it stands on disk
                currentSnapshot = VolumeSnapshot.BuildFromSource(mSource);

                if (!currentSnapshot.Empty)
                {
                    FileSync.__Log(this, "Snapshot built, " + currentSnapshot.FileCount + " files found");

                    VolumeSnapshotRevision mostRecentRevision = mDatabase.GetMostRecentRevision();

                    // Compare to previous snapshot
                    if (mostRecentRevision != null)
                    {
                        FileSync.__Log(this, "Starting incremental backup, " + mDatabase.GetRevisionHistory().Count
                                       + " revisions existing [Most recent: " + mostRecentRevision.ToString() + "]");

                        // Only do an incremental backup to backup whats changed since last full snapshot
                        VolumeSnapshot mostRecentSnapshot = mDatabase.LoadSnapshotRevision(mSource, mostRecentRevision);
                        backupSuccess = mBackupOperation.DoIncrementalBackup(currentSnapshot, mostRecentSnapshot, mSource, mArchive, out noChangesRequired);

                        FileSync.__Log(this, "Incremental backup " + (backupSuccess ? "completed successfully" : "failed")
                                       + " " + (noChangesRequired ? "no changes required" : "changes detected"));
                    }
                    else
                    {
                        FileSync.__Log(this, "Starting full initial backup");

                        // Brand new volume backup, do a full backup of the volume
                        backupSuccess = mBackupOperation.DoFullBackup(currentSnapshot, mSource, mArchive);

                        FileSync.__Log(this, "Full backup " + (backupSuccess ? "completed successfully" : "failed"));
                    }

                    if (backupSuccess)
                    {
                        if (!noChangesRequired)
                        {
                            FileSync.__Log(this, "Saving snapshot revision file [" + currentSnapshot.Revision.ToString() + "]");

                            mDatabase.SaveSnapshotRevision(currentSnapshot);
                            outputToXmlRequired = true;

                            FileSync.__Log(this, "Snapshot file saved");
                        }
                        else
                        {
                            FileSync.__Log(this, "No changes required, not saving revision [" + currentSnapshot.Revision.ToString() + "]");
                        }
                    }
                    else
                    {
                        FileSync.__Log(this, "Backup failed, deleting temporary files for revision [" + currentSnapshot.Revision.ToString() + "]");
                        mDatabase.DeleteSnapshotRevision(currentSnapshot.Revision);                           // remove half-copied data
                        mArchive.DeleteRevision(currentSnapshot.Revision);
                        outputToXmlRequired = true;
                    }
                }
                else
                {
                    FileSync.__Log(this, "Built snapshot is empty, aborting backup");
                }
            }
            catch (Exception e)
            {
                backupSuccess = false;
                FileSync.__LogError(this, "BackupRestoreVolume.DoBackup", e);

                if (currentSnapshot != null)
                {
                    FileSync.__Log(this, "Backup failed due to exception caught, deleting temporary files for revision [" + currentSnapshot.Revision.ToString() + "]");
                    mDatabase.DeleteSnapshotRevision(currentSnapshot.Revision);                       // remove half-copied data
                    mArchive.DeleteRevision(currentSnapshot.Revision);
                }
                outputToXmlRequired = true;
            }
            finally
            {
                mBusy = false;
            }

            return(backupSuccess);
        }
        void MergeSnapshotRevisions( VolumeSnapshotDatabase database, VolumeSource source, BaseArchive archive, int numRevisionsToRemove )
        {
            try
            {
                mDatabase = database;
                mArchive = archive;
                mBusy = true;

                List<VolumeSnapshotRevision> revisionHistory = database.GetRevisionHistory();
                if ( numRevisionsToRemove == revisionHistory.Count )
                {
                    // Need to remove all old revisions, delete everything
                    FileSync.__Log( this, "Merge deleting all revisions" );
                    database.DeleteAllRevisions();
                }
                else
                {
                    // now we know how many revisions to remove, need to rebuild the new first revision.
                    VolumeSnapshotRevision currentRevision = revisionHistory[ numRevisionsToRemove ];
                    VolumeSnapshot currentSnapshot = database.LoadSnapshotRevision( source, currentRevision );

                    FileSync.__Log( this, "Merge is turning revision [" + currentRevision.ToString() + "] into baseline" );
                    TurnSnapshotIntoBaseline( currentSnapshot );

                    // Now go through all existing snapshot .xml files and change any files referencing
                    // a snapshot that has been removed and change it to the current snapshot revision.
                    for ( int revisionNum = numRevisionsToRemove + 1; revisionNum < revisionHistory.Count; ++revisionNum )
                    {
                        VolumeSnapshotRevision incrementalRevision = revisionHistory[ revisionNum ];
                        VolumeSnapshot incrementalSnapshot = database.LoadSnapshotRevision( source, incrementalRevision );

                        FileSync.__Log( this, "Merge is reflecting revision [" + incrementalRevision.ToString() + "] into new baseline [" + currentRevision.ToString()  + "]" );
                        UpdateSnapshotToReflectBaselineRevision( incrementalSnapshot, currentRevision );
                    }

                    // delete old revision data
                    for ( int revisionNum = 0; revisionNum < numRevisionsToRemove; ++revisionNum )
                    {
                        VolumeSnapshotRevision revisionToDelete = revisionHistory[ revisionNum ];
                        FileSync.__Log( this, "Merge is deleting revision [" + revisionToDelete.ToString() + "]" );
                        database.DeleteSnapshotRevision( revisionToDelete );
                    }
                }
            }
            catch ( System.Exception ex )
            {
                FileSync.__LogError( this, "Volume.CheckForExpiredSnapshotRevisions", ex );
            }
            finally
            {
                mBusy = false;
            }
        }