protected string CreateBackup()
    {
        var Url = Request.Url;
        string zipFilename = Url.Scheme + "_" + Url.Host + (Url.IsDefaultPort ? "" : (" " + Url.Port)) + "_" +
                             DateTime.Now.ToString("yyyy_MM.dd_HHmm") + ".zip";
        string zipFilePath = PathCombine(BackupDirectory, zipFilename);

        string backupDirectory = Path.GetDirectoryName(zipFilePath);
        if (!C1Directory.Exists(backupDirectory))
        {
            C1Directory.CreateDirectory(backupDirectory);
        }

        var filenames = GetFilesRecursive(BaseDirectory);
        var directories = GetDirectoriesRecursive(BaseDirectory);

        lock (_lock)
        {
            using(var archiveFileStream = FileCreate(zipFilePath))
            using (var archive = new ZipArchive(archiveFileStream, ZipArchiveMode.Create))
            {
                int oldScriptTimeOut = Server.ScriptTimeout;
                Server.ScriptTimeout = ScriptTimeoutInSeconds;

                foreach (string directory in directories)
                {
                    if (directory.Contains(BackupDirectoryRelativePath))
                        continue;

                    string directoryEntryName = directory.Replace(BaseDirectory, "").Replace("\\", "/") + "/";

                    var dirEntry = archive.CreateEntry(directoryEntryName);
                    dirEntry.LastWriteTime = Directory.GetLastWriteTime(directory);
                }

                foreach (string file in filenames)
                {
                    if (file.Contains(BackupDirectoryRelativePath) || IsTemporaryDirectory(file))
                    {
                        continue;
                    }

                    string fileEntryName = file.Replace(BaseDirectory, "").Replace("\\", "/");

                    var entry = archive.CreateEntry(fileEntryName, BackupCompressionLevel);

                    try
                    {
                        var fi = new C1FileInfo(file);
                        entry.LastWriteTime = fi.LastWriteTime;

                        using (var fileStream = C1File.OpenRead(file))
                        using (var entryStream = entry.Open())
                        {
                            fileStream.CopyTo(entryStream);
                        }
                   }
                    catch (Exception e)
                    {
                        if (!file.Contains("App_Data\\Composite\\LogFiles"))
                        {
                            this.Validators.Add(new ErrorSummary(e.Message));
                        }
                    }
                }

                Server.ScriptTimeout = oldScriptTimeOut;
            }
        }

        return zipFilename;
    }
예제 #2
0
    protected string CreateBackup()
    {
        var Url = Request.Url;
        string zipFilename = Url.Scheme + "_" + Url.Host + (Url.IsDefaultPort ? "" : (" " + Url.Port)) + "_" + DateTime.Now.ToString("yyyy_MM.dd_HHmm") + ".zip";
        string zipFilePath = PathCombine(BackupDirectory, zipFilename);
        if (!C1Directory.Exists(Path.GetDirectoryName(zipFilePath)))
            C1Directory.CreateDirectory(Path.GetDirectoryName(zipFilePath));

        #region Zipping

        var filenames = GetFilesRecursive(BaseDirectory);
        var directories = GetDirectoriesRecursive(BaseDirectory);

        using (ZipOutputStream s = new ZipOutputStream(FileCreate(zipFilePath)))
        {
            lock (_lock)
            {
                int oldScriptTimeOut = Server.ScriptTimeout;
                Server.ScriptTimeout = 600; // 10 minutes

                s.SetLevel(1); // 0 - store only to 9 - means best compression
                byte[] buffer = new byte[4096];

                foreach (string directory in directories)
                {
                    if (directory.Contains(backupDirectoryRelativePath))
                        continue;
                    ZipEntry entry = new ZipEntry(directory.Replace(BaseDirectory, "").Replace("\\", "/") + "/");
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    s.CloseEntry();
                }

                foreach (string file in filenames)
                {
                    if (file.Contains(backupDirectoryRelativePath)
                        || IsTemporaryDirectory(file))
                        continue;
                    ZipEntry entry = new ZipEntry(file.Replace(BaseDirectory, "").Replace("\\", "/"));
                    try
                    {
                        var fi = new C1FileInfo(file);
                        entry.DateTime = fi.LastWriteTime;
                        entry.Size = fi.Length;

                        using (C1FileStream fs = C1File.OpenRead(file))
                        {
                            s.PutNextEntry(entry);
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            } while (sourceBytes > 0);
                        }
                    }
                    catch (Exception e)
                    {
                        if (!file.Contains("App_Data\\Composite\\LogFiles"))
                        {
                            this.Validators.Add(new ErrorSummary(e.Message));
                        }
                    }
                }

                Server.ScriptTimeout = oldScriptTimeOut;
            }
            s.Finish();
            s.Close();
        }
        #endregion
        return zipFilename;
    }
        private static XDocument TryLoad(C1FileInfo candidateFile)
        {
            XDocument dataDocument = null;
            try
            {
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.CheckCharacters = false;

                using (XmlReader xmlReader = XmlReaderUtils.Create(candidateFile.FullName, xmlReaderSettings))
                {
                    dataDocument = XDocument.Load(xmlReader);
                }
            }
            catch (Exception)
            {
                // broken file - should not stop us...
            }

            return dataDocument;
        }