Exemplo n.º 1
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);
        }