Exemplo n.º 1
0
        public ArchiveCreated Backup(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException(@"Value cannot be null or empty.", nameof(id));
            }

            Configuration current = _repository.Get(id);

            if (current == null)
            {
                return(null);
            }

            return(_archive.Archive(current.Name, archive =>
            {
                archive.Options
                .GroupedBy("Backup")
                .ExpiresAfterMonths(1)
                .Compression(CompressionLevel.Optimal);

                archive.IncludeContent("data", current.JsonData, ".json");
                archive.IncludeContent("meta", string.Join(Environment.NewLine,
                                                           current.Id,
                                                           current.Name,
                                                           current.Description,
                                                           current.Updated.ToString(),
                                                           current.UpdatedBy));
            }));
        }
        public override void Execute(ITaskExecutionContext <MaintenanceWorkItem> context)
        {
            MaintenanceConfiguration configuration = _configuration.Get <MaintenanceConfiguration>();

            DateTimeOffset tasksLowerBound  = Time.UtcNow.Subtract(context.WorkItem.Configuration.CleanUpTaskLogEntriesOlderThan),
                           errorsLowerBound = Time.UtcNow.Subtract(context.WorkItem.Configuration.CleanUpErrorLogEntriesOlderThan);

            using (IDbSession session = _db.Value.OpenSession())
            {
                Tuple <int, string> taskLog  = DeleteEntries(configuration, session, IntegrationDbTable.TaskLog, tasksLowerBound);
                Tuple <int, string> errorLog = DeleteEntries(configuration, session, IntegrationDbTable.ErrorLog, errorsLowerBound);

                if (taskLog.Item1 > 0 || errorLog.Item1 > 0)
                {
                    ArchiveCreated archive = null;

                    if (configuration.ArchiveDeletedLogEntries)
                    {
                        archive = _archiver.Archive("IntegrationDb-Maintenance", a =>
                        {
                            a.Options
                            .GroupedBy("Backup")
                            .ExpiresAfterMonths(12)
                            .Compression(CompressionLevel.Optimal);

                            a.IncludeContent($"TaskLog_{tasksLowerBound:yyyyMMdd}.csv", taskLog.Item2);
                            a.IncludeContent($"ErrorLog_{errorsLowerBound:yyyyMMdd}.csv", errorLog.Item2);
                        });
                    }

                    context.Log.Message(@"Deleted {0} task entries older than '{1}'. 
Deleted {2} error entries older than '{3}'
Archive: {4}",
                                        taskLog.Item1,
                                        tasksLowerBound,
                                        errorLog.Item1,
                                        errorsLowerBound,
                                        archive ?? "<Not archived>");
                }
            }
        }
Exemplo n.º 3
0
        public string[] Upload(IEnumerable <FtpFile> files, Action <ArchiveCreated> onArchived = null)
        {
            if (files == null)
            {
                throw new ArgumentNullException(nameof(files));
            }

            ArchiveOptions archiveOptions = _configuration.ArchiveOptions;

            if (archiveOptions != null)
            {
                var archive = _archive.Archive(archiveOptions.Name, newArchive =>
                {
                    newArchive.Options.GroupedBy(archiveOptions.GroupName);

                    if (archiveOptions.Expires.HasValue)
                    {
                        newArchive.Options.ExpiresOn(archiveOptions.Expires.Value);
                    }

                    foreach (FtpFile file in files)
                    {
                        newArchive.IncludeContent(file.FileName, file.Contents);
                    }
                });

                onArchived?.Invoke(archive);
            }

            IFtpClient ftpClient = _ftpClientFactory.Create(_configuration.FtpConnectionString);

            var encoding = new UTF8Encoding(false /* to ensure that Globase can read the file, this parameter is necessary */);

            return(files
                   .Select(file => ftpClient.UploadFromString(file.FileName, file.Contents, encoding))
                   .ToArray());
        }
Exemplo n.º 4
0
        public override void Execute(ITaskExecutionContext <MaintenanceWorkItem> context)
        {
            MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder[] folders =
                context.WorkItem.Configuration.ArchiveFolders.GetEnabledFolders();

            foreach (MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder folder in folders)
            {
                context.Log.Message("Folder: {0}", folder);

                FileInfo[]      filesToArchive   = folder.GetFiles() ?? new FileInfo[0];
                DirectoryInfo[] foldersToArchive = folder.GetFolders() ?? new DirectoryInfo[0];

                if (filesToArchive.Length == 0 && foldersToArchive.Length == 0)
                {
                    continue;
                }

                MaintenanceConfiguration.ArchiveFoldersConfiguration.Folder localFolder = folder;

                ArchiveCreated archive = _archiver.Archive(folder.ArchiveOptions.Name, a =>
                {
                    if (localFolder.ArchiveOptions.Expires.HasValue)
                    {
                        a.Options.ExpiresOn(localFolder.ArchiveOptions.Expires.Value);
                    }

                    if (localFolder.ArchiveOptions.CompressionLevel.HasValue)
                    {
                        a.Options.Compression(localFolder.ArchiveOptions.CompressionLevel.Value);
                    }

                    a.Options.GroupedBy(localFolder.ArchiveOptions.GroupName);

                    foreach (FileInfo fileToArchive in filesToArchive)
                    {
                        a.IncludeFile(fileToArchive);
                    }

                    foreach (DirectoryInfo folderToArchive in foldersToArchive)
                    {
                        a.IncludeFolder(folderToArchive);
                    }
                });

                context.Log.Message("{0} file(s) and {1} folder(s) have been archived and will now be physically deleted. {2}.",
                                    filesToArchive.Length, foldersToArchive.Length, archive);

                foreach (FileInfo fileToDelete in filesToArchive)
                {
                    try
                    {
                        fileToDelete.Delete();
                    }
                    catch (Exception ex)
                    {
                        context.Log.Error(folder.Target, $"Unable to delete file '{fileToDelete.FullName}'. Message: {ex.Message}");
                    }
                }

                foreach (DirectoryInfo folderToDelete in foldersToArchive)
                {
                    try
                    {
                        folderToDelete.Delete(recursive: true);
                    }
                    catch (Exception ex)
                    {
                        context.Log.Error(folder.Target, $"Unable to delete folder '{folderToDelete.FullName}'. Message: {ex.Message}");
                    }
                }
            }
        }