Example #1
0
        public void TestBackup()
        {
            var engine = new CloudBackup.Backup.Engine();

            engine.RootPath = @"C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\";

            using (var backup = engine.CreateBackup())
            {
                backup.CreateShadowCopy();

                var snapRoot = backup.GetSnapshotPath();
                var ret = backup.GetSnapshotElements();

                backup.DropBackup();

            }


        }
Example #2
0
        void PublishZipFile(Engine.IBackup backup)
        {
            log.InfoFormat("Process completed - Streaming ZIP file");

            _report.AppendLine();
            _report.AppendLine(" * Streaming ZIP file to target");

            var swTimer = new Stopwatch();
            swTimer.Start();

            bool transfertIsSuccess;
            Exception transferException;
            if (_backend is FTP)
            {
                try
                {
                    using (var sendStream = ((FTP)_backend).GetWriteStream(_fileName))
                    {
                        _zipFile.Save(sendStream);
                    }
                    transferException = null;
                    transfertIsSuccess = true;
                }
                catch (Exception ex)
                {
                    transferException = ex;
                    transfertIsSuccess = false;
                }
            }
            else
            {
                Stream write, read;
                PipeStream.CreatePipe(out write, out read);

                var sshThread = new Thread(SshSinkThread);
                var param = new SshSinkParams { Target = _backend, Stream = read, File = _fileName };
                sshThread.Start(param);
                _zipFile.Save(write);
                write.Dispose();
                log.InfoFormat("Process completed - Joining");
                sshThread.Join();

                transfertIsSuccess = param.IsSuccess;
                transferException = param.Exception;
            }

            swTimer.Stop();

            log.InfoFormat("Process completed in {0} - Droping backup", swTimer.Elapsed);
            _report.AppendLine("   Total time for transfert : " + swTimer.Elapsed);
            backup.DropBackup();

            if (!transfertIsSuccess)
            {
                log.ErrorFormat("File transfert is not successfull");
                throw new Exception("Transfert failed", transferException);
            }
        }
Example #3
0
        public static void RunBackup(ArchiveJob job,bool forceFullBackup, string reason)
        {
            log.InfoFormat("Starting backup of [{0}]", job.JobRootPath);

            using (var process = new Process(job, forceFullBackup, reason))
            {
                try
                {
                    process.BuildConnectBackend();

                    var engine = new Engine { RootPath = job.JobRootPath };
                    using (var backup = engine.CreateBackup())
                    {
                        //- First create a snapshot
                        process.BuildSnapshot(backup);

                        //- Publish ZIP file
                        process.PublishZipFile(backup);
                    }

                    log.InfoFormat("Process completed - Commit Transaction");
                    process.CommitTransaction();

                    // Post process
                    if (job.JobTarget.ManageTargetFiles)
                    {
                        process.ClearUnneededFiles();
                    }
                }
                catch(Exception ex)
                {
                    process._proxy.Transaction.Rollback();
                    process._proxy = null;

                    log.Error("Process failed ! - Rollback Transaction", ex);
                    process._report.AppendLine("** FAILED ** Exception: " + ex);

                    var proxy = Program.Database.CreateSnapProxy();
                    proxy.BackupReport(job.JobUID.Value, process._now.Ticks, false,reason, process._report.ToString());
                    proxy.Transaction.Commit();
                }
            }
        }
Example #4
0
        private void BuildSnapshot(Engine.IBackup backup)
        {
            log.Debug("Building backup snapshot");

            var manifest = new ManifestDocument(_job, _id);
            manifest.AddTag("date", XmlConvert.ToString(_now, XmlDateTimeSerializationMode.Utc));
            manifest.AddTag("source", Environment.MachineName);
            manifest.AddTag("from", _job.JobRootPath);
            manifest.AddTag("fullBackup", XmlConvert.ToString(_forceFullBackup));

            var jid = _job.JobUID.Value;

            if (_forceFullBackup)
            {
                _report.AppendLine("Note: Running a full backup");
                _proxy.ClearAllFiles(jid);
            }

            backup.CreateShadowCopy();
            var rootPath = backup.GetSnapshotPath();
            var elements = backup.GetSnapshotElements();

            _report.AppendLine();
            _report.AppendLine(" * Backup process :");
            _report.AppendLine();

            var countNewFiles = 0L;
            var countChangeFiles = 0L;
            var countDeletedFiles = 0L;

            var hash = 0L;
            foreach (var element in elements)
            {
                var file = _proxy.FindFile(jid, element.Path);
                if (element.IsDirectory)
                {
                    log.InfoFormat("[{0}] - Directory - Add entry", element.Path);
                    var entry = _zipFile.AddDirectoryByName(element.Path);
                    entry.CreationTime = element.Created;
                    entry.AccessedTime = element.LastAccessed;
                    entry.ModifiedTime = element.LastModified;

                    if (file != null)
                        _proxy.SetSeenFlags(jid, element.Path);
                    else
                        _proxy.AddFile(
                            element.Path, jid,
                            element.FileSize,
                            element.Created.Ticks,
                            element.LastModified.Ticks,
                            0, _id
                            );

                    continue;
                }

                if (file == null)
                {
                    log.InfoFormat("[{0}] - New file / Archive", element.Path);
                    if (element.FileSize != 0)
                    {
                        var entryDoc = new SnapshotAccess(rootPath + element.Path);
                        var entry = _zipFile.AddEntry(element.Path, entryDoc.OpenDelegate, entryDoc.CloseDelegate);
                        entry.CreationTime = element.Created;
                        entry.AccessedTime = element.LastAccessed;
                        entry.ModifiedTime = element.LastModified;
                        hash = entry.Crc;
                    }
                    else
                    {
                        hash = 0L;
                        manifest.NotifyEmptyFile(element.Path);
                    }

                    _report.AppendLine("Backup new file " + element.Path);
                    ++countNewFiles;
                    _proxy.AddFile(
                        element.Path, jid,
                        element.FileSize,
                        element.Created.Ticks,
                        element.LastModified.Ticks,
                        hash,
                        _id
                        );
                    continue;
                }

                if (
                    file.Modified == element.LastModified.Ticks &&
                    file.Created == element.Created.Ticks &&
                    file.FileSize == element.FileSize
                    )
                {
                    log.DebugFormat("[{0}] - No change / No archive", element.Path);
                    _proxy.SetSeenFlags(jid, element.Path);
                    continue;
                }

                log.InfoFormat("[{0}] - File changed / Archiving", element.Path);
                _report.AppendLine("Backup modified file " + element.Path);
                ++countChangeFiles;

                if (element.FileSize != 0)
                {
                    var entryDoc2 = new SnapshotAccess(rootPath + element.Path);
                    var entry2 = _zipFile.AddEntry(element.Path, entryDoc2.OpenDelegate, entryDoc2.CloseDelegate);
                    entry2.CreationTime = element.Created;
                    entry2.AccessedTime = element.LastAccessed;
                    entry2.ModifiedTime = element.LastModified;
                    hash = entry2.Crc;
                }
                else
                {
                    hash = 0L;
                    manifest.NotifyEmptyFile(element.Path);
                }

                _proxy.UpdateFile(
                    element.Path, jid,
                    element.FileSize,
                    element.Created.Ticks,
                    element.LastModified.Ticks,
                    hash,
                    _id
                    );
            }

            using (var delFiles = _proxy.GetDeletedFiles(jid))
            {
                while (delFiles.MoveNext())
                {
                    _report.AppendLine("Deleted file " + delFiles.Current.SourcePath);
                    ++countDeletedFiles;
                    manifest.NotifyDelFile(delFiles.Current.SourcePath, delFiles.Current.FileSize);
                }
            }

            _report.AppendLine();
            _report.AppendLine(string.Format(" * Report: New files: {0} / Modified files: {1} / Deleted files: {2}", countNewFiles, countChangeFiles, countDeletedFiles));

            _zipFile.Comment = manifest.Manifest;
        }