public static long ExportCatalogUpdates(IRFProcessingContext context, string path, RFDate startDate, RFDate?endDate = null, string password = null)
        {
            long c = 0;

            var keysInScope = context.SearchKeys(typeof(RFCatalogKey), startDate, endDate, 99999, null, false).Where(k => k.IsValid && k.Key.Plane == RFPlane.User).ToList();

            foreach (var keyDate in keysInScope.GroupBy(k => k.UpdateTime.Date))
            {
                var fileName = Path.Combine(path, String.Format("RIFF_{0}_Updates_{1}.zip", context.Environment, keyDate.Key.ToString("yyyyMMdd")));

                context.SystemLog.Info(typeof(RFCatalogMaintainer), "Exporting {0} documents into {1}", keyDate.Count(), fileName);

                var  exportableDocuments = new Dictionary <string, byte[]>();
                long cnt = 1;
                foreach (var key in keyDate)
                {
                    var doc = context.LoadEntry(key.Key) as RFDocument;
                    if (doc != null)
                    {
                        var docName = RFFileHelpers.SanitizeFileName(string.Format("{0}_{1}_{2}_{3}_{4}.xml",
                                                                                   doc.Key.GraphInstance?.ValueDate?.ToString() ?? "none",
                                                                                   doc.Key.GraphInstance?.Name ?? "none",
                                                                                   doc.Key.GetType().Name,
                                                                                   doc.Key.FriendlyString(),
                                                                                   cnt++));
                        exportableDocuments.Add(docName, Encoding.UTF8.GetBytes(RFXMLSerializer.PrettySerializeContract(doc)));
                        c++;
                    }
                }
                ZIPUtils.ZipFiles(fileName, exportableDocuments, password);
            }
            return(c);
        }
        public static long ImportCatalogUpdates(IRFProcessingContext context, string path)
        {
            long c = 0;

            foreach (var f in Directory.GetFiles(path, "*.zip").OrderBy(f => f))
            {
                context.SystemLog.Info(typeof(RFCatalogMaintainer), "Importing updates from {0}", f);
                using (var fs = new FileStream(f, FileMode.Open, FileAccess.Read))
                {
                    try
                    {
                        foreach (var entry in ZIPUtils.UnzipArchive(fs))
                        {
                            try
                            {
                                var document = RFXMLSerializer.DeserializeContract(typeof(RFDocument).FullName, new string(Encoding.UTF8.GetChars(entry.Item2))) as RFDocument;
                                if (document != null && context.SaveEntry(document, false, false))
                                {
                                    c++;
                                }
                            }
                            catch (Exception ex)
                            {
                                context.SystemLog.Error(typeof(RFCatalogMaintainer), "Error importing entry {0}: {1}", entry.Item1, ex.Message);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        context.SystemLog.Error(typeof(RFCatalogMaintainer), "Error processing .zip file {0}: {1}", f, ex.Message);
                    }
                }
            }
            return(c);
        }
Beispiel #3
0
        protected bool BackupDatabase()
        {
            string databaseName     = "n/a";
            string workingPathLocal = null;
            string workingPathUNC   = null;
            string backupPath       = null;

            try
            {
                if (!string.IsNullOrWhiteSpace(_config.ConnectionString) && _config.MaintainDatabase && !string.IsNullOrWhiteSpace(_config.BackupDirectory))
                {
                    using (var connection = new SqlConnection(_config.ConnectionString))
                    {
                        connection.Open();

                        databaseName = connection.Database;
                        var backupFileName = databaseName;
                        if (_config.WeeklyRotation)
                        {
                            backupFileName = String.Format("{0}_{1}", backupFileName, DateTime.Today.DayOfWeek);
                        }
                        backupFileName += ".bak";

                        if (!string.IsNullOrWhiteSpace(_config.BackupPassword))
                        {
                            // save to working directory and encrypt
                            var workingDirectory = _config.WorkingDirectoryLocal ?? Path.GetTempPath();
                            workingPathLocal = Path.Combine(_config.WorkingDirectoryLocal, backupFileName);
                            workingPathUNC   = Path.Combine(_config.WorkingDirectoryUNC, backupFileName);
                            BackupDatabaseTo(connection, databaseName, workingPathLocal);
                            ZIPUtils.ZipFile(_config.WorkingDirectoryUNC, backupFileName, _config.BackupDirectory, _config.BackupPassword);
                            File.Delete(workingPathUNC);
                            Log.Info("Backed up and encrypted database {0} to {1}.", databaseName, backupFileName);
                        }
                        else
                        {
                            // save directly to destination
                            backupPath = Path.Combine(_config.BackupDirectory, backupFileName);
                            BackupDatabaseTo(connection, databaseName, backupPath);
                            Log.Info("Backed up database {0} to {1}.", databaseName, backupFileName);
                        }
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Log.SystemError(ex, "Error backing up database {0} into {1}", databaseName, _config.BackupDirectory);
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(workingPathLocal) && File.Exists(workingPathLocal))
                {
                    try
                    {
                        File.Delete(workingPathLocal);
                    }
                    catch (Exception ex)
                    {
                        Log.Warning("Unable to remove local working file {0}: {1}", workingPathLocal, ex.Message);
                    }
                }
                if (!string.IsNullOrWhiteSpace(workingPathUNC) && File.Exists(workingPathUNC))
                {
                    try
                    {
                        File.Delete(workingPathUNC);
                    }
                    catch (Exception ex)
                    {
                        Log.Warning("Unable to remove UNC working file {0}: {1}", workingPathLocal, ex.Message);
                    }
                }
            }
            return(false);
        }