Пример #1
0
        public static void Perform(Player p, bool files, bool db, bool lite, bool compress)
        {
            if (db)
            {
                Logger.Log(LogType.SystemActivity, "Backing up the database...");
                using (StreamWriter sql = new StreamWriter(sqlPath))
                    BackupDatabase(sql);
                Logger.Log(LogType.SystemActivity, "Backed up the database to " + sqlPath);
            }

            List <string> filesList = null;

            if (files)
            {
                Logger.Log(LogType.SystemActivity, "Determining which files to backup...");
                filesList = GetAllFiles(lite);
                Logger.Log(LogType.SystemActivity, "Finished determining included files");
            }

            Logger.Log(LogType.SystemActivity, "Creating compressed backup...");
            using (Stream stream = File.Create(zipPath)) {
                ZipWriter writer = new ZipWriter(stream);
                if (files)
                {
                    SaveFiles(writer, filesList, compress);
                }
                if (db)
                {
                    SaveDatabase(writer, compress);
                }

                writer.FinishEntries();
                writer.WriteFooter();
                Logger.Log(LogType.SystemActivity, "Compressed all data!");
            }
            p.Message("Backup of (" + (files ? "everything" + (db ? "" : " but database") : "database") + ") complete!");
            Logger.Log(LogType.SystemActivity, "Server backed up!");
        }
Пример #2
0
        static void SaveFiles(ZipWriter writer, List <string> paths, bool compress)
        {
            Logger.Log(LogType.SystemActivity, "Compressing {0} files...", paths.Count);
            for (int i = 0; i < paths.Count; i++)
            {
                string path         = paths[i];
                bool   compressThis = compress && !path.CaselessContains(".lvl");

                try {
                    using (Stream src = File.OpenRead(path)) {
                        writer.WriteEntry(src, path, compressThis);
                    }
                } catch (Exception ex) {
                    Logger.LogError("Failed to backup file: " + path, ex);
                }

                if (i == 0 || (i % 100) != 0)
                {
                    continue;
                }
                Logger.Log(LogType.SystemActivity, "Backed up {0}/{1} files", i, paths.Count);
            }
        }