/// <summary>
    /// Export the GitBucket database as a plain text dump using pg_dump.
    /// </summary>
    /// <param name="options">The BackupOptions.</param>
    /// <param name="connectionString">The connectionString.</param>
    private void DumpDatabase(BackupOptions options, string connectionString)
    {
        _console.WriteLine("Database backup");

        var builder    = new NpgsqlConnectionStringBuilder(connectionString);
        var outputFile = Path.Combine(options.Destination, $"{builder.Database}_{DateTime.Now:yyyyMMddHHmmss}.sql");

        using var process                        = new Process();
        process.StartInfo.FileName               = string.IsNullOrEmpty(options.PgDump) ? "pg_dump" : options.PgDump;
        process.StartInfo.Arguments              = $"--host={builder.Host} --port={builder.Port} --username={builder.Username} --dbname={builder.Database} --file={outputFile} --format=plain";
        process.StartInfo.UseShellExecute        = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.Environment.Add("PGPASSWORD", builder.Password);

        try
        {
            process.Start();
            var output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            _console.WriteLine($"The database dump is available in {outputFile}");
        }
        catch (Exception ex)
        {
            _console.WriteWarnLine(ex.ToString());
        }
    }
        public async Task Create_One_Backup_File_Mongo()
        {
            // Arrange
            if (!_context.AllowMongoDB)
            {
                Assert.True(true);
                return;
            }

            BackupOptions backupOptions = new BackupOptions
            {
                BackupFolderPath = "."
            };

            BackupMongoRepository backupRepository = new BackupMongoRepository(_context.GetMongoConnection());
            IBackupService        backupService    = GetMockBackupService(backupRepository, backupOptions);

            // Act
            LetPortal.Portal.Models.Recoveries.BackupResponseModel result = await backupService.CreateBackupFile(new LetPortal.Portal.Models.Recoveries.BackupRequestModel
            {
                Name        = "BK_Test",
                Description = "BK Test",
                Creator     = "Admin"
            });

            backupRepository.Dispose();
            // Assert
            Assert.True(!string.IsNullOrEmpty(result.DownloadableUrl));
        }
    /// <summary>
    /// Check if pg_dump is available.
    /// </summary>
    /// <param name="options">The BackupOptions.</param>
    /// <returns>true if available, false if not.</returns>
    private bool PgDumpAvailable(BackupOptions options)
    {
        _console.WriteLine("Checking pg_dump existence...");

        using var process                        = new Process();
        process.StartInfo.FileName               = string.IsNullOrEmpty(options.PgDump) ? "pg_dump" : options.PgDump;
        process.StartInfo.Arguments              = "--help";
        process.StartInfo.UseShellExecute        = false;
        process.StartInfo.RedirectStandardOutput = true;

        try
        {
            process.Start();
            var output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            if (process.ExitCode != 0 || !output.Contains("pg_dump [OPTION]... [DBNAME]", StringComparison.OrdinalIgnoreCase))
            {
                _console.WriteWarnLine($"Cannot found valid pg_dump.");
                return(false);
            }

            return(true);
        }
        catch (Exception ex)
        {
            _console.WriteWarnLine(ex.ToString());
            return(false);
        }
    }
    /// <summary>
    /// Clone all repositories, comments, diff, lfs-files, releases into repositoryBackupFolder.
    /// </summary>
    /// <param name="options">The BackupOptions.</param>
    private void CloneRepositories(BackupOptions options)
    {
        _console.WriteLine("Starting clone process");

        foreach (var ownerPath in Directory.EnumerateDirectories(options.GitBucketHomeRepositoriesDir, "*", SearchOption.TopDirectoryOnly))
        {
            var owner = new DirectoryInfo(ownerPath).Name;
            Directory.CreateDirectory(Path.Combine(options.DestinationRepositoriesDir, owner));

            var repositories = Directory.EnumerateDirectories(ownerPath, "*", SearchOption.TopDirectoryOnly);
            foreach (var repositoryPath in repositories)
            {
                var repository = new DirectoryInfo(repositoryPath).Name;
                if (repository.Contains(".git", StringComparison.OrdinalIgnoreCase))
                {
                    // Backup git bare repository
                    CreateClone(owner, repository, options);
                }
                else
                {
                    // Backup comments, diff, lfs, releases
                    CopyEntireDirectory(repositoryPath, Path.Combine(options.DestinationRepositoriesDir, owner, repository));
                }
            }
        }

        _console.WriteLine("All repositories cloned");
    }
Example #5
0
        private static void AddConfigurationServices(IServiceCollection services)
        {
            services.AddSingleton(configuration);
            services.AddSingleton <TeamsConfiguration>(ctx =>
            {
                var t = new TeamsConfiguration();
                configuration.Bind("Teams", t);
                return(t);
            });

            services.AddSingleton <BackupOptions>(ctx =>
            {
                var t = new BackupOptions();
                configuration.Bind("Backup", t);
                return(t);
            });

            services.AddSingleton <MySqlOptions>(ctx =>
            {
                var t = new MySqlOptions();
                configuration.Bind("MySql", t);
                return(t);
            });

            services.AddSingleton <RetentionOptions>(ctx =>
            {
                var t = new RetentionOptions();
                configuration.Bind("Retention", t);
                return(t);
            });

            services.AddSingleton <SlackConfiguration>(ctx =>
            {
                var t = new SlackConfiguration();
                configuration.Bind("Slack", t);
                return(t);
            });

            services.AddSingleton <AzureConfiguration>(ctx =>
            {
                var t = new AzureConfiguration();
                configuration.Bind("Azure", t);
                return(t);
            });

            services.AddSingleton <S3Configuration>(ctx =>
            {
                var t = new S3Configuration();
                configuration.Bind("S3", t);
                return(t);
            });

            services.AddSingleton <PhysicalConfiguration>(ctx =>
            {
                var t = new PhysicalConfiguration();
                configuration.Bind("Physical", t);
                return(t);
            });
        }
Example #6
0
        /// <summary>
        /// 增量备份
        /// </summary>
        /// <param name="dir"></param>
        public void SetincrementalBackUP(string dir)
        {
            if (string.IsNullOrWhiteSpace(dir))
            {
                dir = db_backupdir;
            }
            BackupOptions options = new BackupOptions();

            options.Creation = CreatePolicy.IF_NEEDED;
            options.Files    = true;
            options.Update   = true;
            env.Backup(dir, options);
        }
Example #7
0
        public async Task Backup(BackupOptions options = BackupOptions.None, IEnumerable <string> packages = null, string backupFile = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            string fileString = backupFile != null?string.Format("-f {0}", backupFile.QuoteIfNeeded()) : "";

            string packagesString = "";

            if (packages != null)
            {
                packagesString = packages.Aggregate("", (acc, p) => acc + " " + p);
            }

            await new Adb(this, "backup {0} {1} {2}", fileString, options.GenerateString(), packagesString).RunAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
        }
 /// <summary>
 /// Update a cloned folder, the update is down toward the latest state of its default remote.
 /// As "git remote update" is not implemented in LibGit2Sharp yet, using "git fetch" instead.
 /// https://github.com/libgit2/libgit2sharp/issues/1466
 /// </summary>
 /// <param name="options">The BackupOptions.</param>
 private void UpdateRepositories(BackupOptions options)
 {
     foreach (var owener in Directory.EnumerateDirectories(options.DestinationRepositoriesDir))
     {
         foreach (var repositoryPath in Directory.EnumerateDirectories(owener, "*.git", SearchOption.TopDirectoryOnly))
         {
             _console.WriteLine($"updating {repositoryPath}");
             using var repo = new LibGit2Sharp.Repository(repositoryPath);
             var remote   = repo.Network.Remotes["origin"];
             var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
             Commands.Fetch(repo, remote.Name, refSpecs, null, null);
         }
     }
 }
Example #9
0
        public async Task Backup(BackupOptions options, string expected)
        {
            // arrange
            var processManager = new TestProcessManager();

            processManager.AddProcess(new[] { "Success" });
            var adb = new Adb(processManager);

            // act
            var deviceAdb = adb.SingleDevice;
            await deviceAdb.Backup(options);

            // assert
            processManager.Stub.AssertWasCalled(_ => _.CreateProcess(Arg <string> .Is.Anything, Arg <string> .Matches(c => c.Contains(expected))));
        }
Example #10
0
    /// <summary>
    /// To keep integrity as its maximum possible, the database export and git backups must be done in the shortest possible timeslot.
    /// Thus we will:
    ///  - clone new repositories into backup directory
    ///  - update them all a first time, so that we gather updates
    ///  - export the database
    ///  - update all repositories a second time, this time it should be ultra-fast
    /// </summary>
    /// <param name="options">The BackupOptions.</param>
    /// <param name="connectionString">The connectionString.</param>
    /// <returns>Return code.</returns>
    public int Backup(BackupOptions options, string connectionString)
    {
        ArgumentNullException.ThrowIfNull(options);

        Directory.CreateDirectory(options.Destination);
        Directory.CreateDirectory(options.DestinationRepositoriesDir);

        CloneRepositories(options);

        _console.WriteLine("Update repositories: phase 1");
        UpdateRepositories(options);
        _console.WriteLine("Update repositories: phase 1, terminated");

        if (PgDumpAvailable(options))
        {
            DumpDatabase(options, connectionString);
        }

        // Export the GitBucket configuration
        _console.WriteLine("Configuration backup");
        if (File.Exists(options.GitBucketHomeConfigurationFile))
        {
            File.Copy(options.GitBucketHomeConfigurationFile, options.DestinationConfigurationFile, overwrite: true);
        }

        // Export the GitBucket database configuration
        _console.WriteLine("Database configuration backup");
        File.Copy(options.GitBucketHomeDatabaseConfigurationFile, options.DestinationDatabaseConfigurationFile, overwrite: true);

        // Export the GitBucket data directory (avatars, ...)
        // data directory exists only if there are some files uploaded by users.
        if (Directory.Exists(options.GitBucketHomeDataDir))
        {
            _console.WriteLine("Avatars backup");
            CopyEntireDirectory(options.GitBucketHomeDataDir, options.DestinationDataDir);
        }

        _console.WriteLine("Plugins backup");
        CopyEntireDirectory(options.GitBucketPluginsDataDir, options.DestinationPluginsDir);

        _console.WriteLine("Update repositories: phase 2");
        UpdateRepositories(options);
        _console.WriteLine("Update repositories: phase 2, terminated");

        _console.WriteLine($"Update process ended, backup available under: {options.DestinationRepositoriesDir}");
        return(0);
    }
        public async Task Preview_Backup_Mongo()
        {
            // Arrange
            if (!_context.AllowMongoDB)
            {
                Assert.True(true);
                return;
            }

            Mock <IFormFile> mockFile     = new Mock <IFormFile>();
            FileStream       sourceZip    = System.IO.File.OpenRead(@"Artifacts\" + zipFileName);
            MemoryStream     memoryStream = new MemoryStream();
            await sourceZip.CopyToAsync(memoryStream);

            sourceZip.Close();
            memoryStream.Position = 0;
            string fileName = zipFileName;

            mockFile.Setup(f => f.Length).Returns(memoryStream.Length).Verifiable();
            mockFile.Setup(f => f.FileName).Returns(fileName).Verifiable();
            mockFile.Setup(f => f.OpenReadStream()).Returns(memoryStream).Verifiable();
            mockFile
            .Setup(f => f.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns((Stream stream, CancellationToken token) => memoryStream.CopyToAsync(stream))
            .Verifiable();
            BackupOptions backupOptions = new BackupOptions
            {
                BackupFolderPath  = "Backup",
                MaximumObjects    = 0,
                RestoreFolderPath = "Restore"
            };

            BackupMongoRepository backupRepository = new BackupMongoRepository(_context.GetMongoConnection());
            IBackupService        backupService    = GetMockBackupService(backupRepository, backupOptions);

            // Act
            UploadBackupResponseModel backupResponse = await backupService.UploadBackupFile(mockFile.Object, "Admin");

            LetPortal.Portal.Models.Recoveries.PreviewRestoreModel result = await backupService.PreviewBackup(backupResponse.Id);

            memoryStream.Close();
            memoryStream.Dispose();
            backupRepository.Dispose();

            // Assert
            Assert.NotNull(result);
        }
Example #12
0
        static bool GatherVolume(string volume, ref BackupOptions opts)
        {
            ZfsSnapshots snaps = new ZfsSnapshots();

            if (!snaps.Filesystems().Contains(volume))
            {
                Console.WriteLine("No snapshots for that volume, or no such volume.");
                return(false);
            }

            opts.Filesystem           = volume;
            opts.Filename             = volume.Replace('/', '_');
            opts.IncrementalStartDate = null;
            opts.SnapshotDate         = snaps.Snapshots(volume).OrderByDescending(s => s.Date).First().Date.ToString("yyyy-MM-dd");

            return(true);
        }
Example #13
0
    /// <summary>
    /// Create a git mirror clone of a repository. If the clone already exists, the operation is skipped.
    /// </summary>
    /// <param name="owner">The owner of the repository to be cloned.</param>
    /// <param name="repository">The repository name to be cloned.</param>
    /// <param name="options">The BackupOptions.</param>
    private void CreateClone(string owner, string repository, BackupOptions options)
    {
        var dest = Path.Combine(options.DestinationRepositoriesDir, owner, repository);

        if (Directory.Exists(dest))
        {
            _console.WriteLine($"{dest} already exists, skipping git clone operation");
            return;
        }

        var source = Path.Combine(options.GitBucketHomeRepositoriesDir, owner, repository);

        _console.WriteLine($"cloning {source} into {dest}");

        LibGit2Sharp.Repository.Clone(source, dest, new CloneOptions {
            IsBare = true
        });
    }
Example #14
0
        public static string GenerateString(this BackupOptions options)
        {
            string optionsString = string.Empty;

            if ((options & BackupOptions.Apk) != 0)
            {
                optionsString += " -apk";
            }
            if ((options & BackupOptions.NoApk) != 0)
            {
                optionsString += " -noapk";
            }
            if ((options & BackupOptions.Obb) != 0)
            {
                optionsString += " -obb";
            }
            if ((options & BackupOptions.NoObb) != 0)
            {
                optionsString += " -noobb";
            }
            if ((options & BackupOptions.Shared) != 0)
            {
                optionsString += " -shared";
            }
            if ((options & BackupOptions.NoShared) != 0)
            {
                optionsString += " -noshared";
            }
            if ((options & BackupOptions.All) != 0)
            {
                optionsString += " -all";
            }
            if ((options & BackupOptions.System) != 0)
            {
                optionsString += " -system";
            }
            if ((options & BackupOptions.NoSystem) != 0)
            {
                optionsString += " -nosystem";
            }

            return(optionsString);
        }
 public LogCollectionBackupController(
     ICollectionFacade collectionFacade,
     IStorageFacade storageFacade,
     TimeSpan rpo,
     BackupOptions included,
     LogConstants logConstants,
     ILogger logger)
 {
     _collection = collectionFacade;
     _logFile    = new LogFile(
         storageFacade,
         collectionFacade.Parent.Parent.AccountName,
         collectionFacade.Parent.DatabaseName,
         collectionFacade.CollectionName,
         logger);
     _rpo          = rpo;
     _included     = included;
     _logConstants = logConstants;
     _logger       = logger;
 }
Example #16
0
        public static void GenerateBackup(MySqlConnector conn, Stream outputStream, BackupOptions options)
        {
            using (StreamWriter writer = new StreamWriter(outputStream, Encoding.UTF8))
            {
                if (options.BOM)
                {
                    outputStream.Write(System.Text.Encoding.UTF8.GetPreamble(), 0, System.Text.Encoding.UTF8.GetPreamble().Length);
                }
                writer.Write(string.Format(@"DELIMITER {0}{1}", DELIMITER, NEW_LINE));
                writer.Write(string.Format(@"SET FOREIGN_KEY_CHECKS=0 {0}{1}", DELIMITER, NEW_LINE));
                writer.Write(string.Format(@"SET SQL_MODE=""NO_AUTO_VALUE_ON_ZERO"" {0}{1}", DELIMITER, NEW_LINE));
                writer.Write(string.Format(@"SET AUTOCOMMIT=0 {0}{1}", DELIMITER, NEW_LINE));
                if (options.WrapInTransaction) writer.Write(string.Format(@"START TRANSACTION {0}{1}", DELIMITER, NEW_LINE));

                if (options.ExportRoutines) ExportRoutines(conn, writer);
                if (options.ExportTableCreate) ExportTableCreate(conn, writer, options.ExportTableDrop);
                if (options.ExportTableData) ExportTableData(conn, writer, options);
                if (options.ExportTriggers) ExportTriggers(conn, writer);

                writer.Write(string.Format(@"SET FOREIGN_KEY_CHECKS=1 {0}{1}", DELIMITER, NEW_LINE));
                if (options.WrapInTransaction) writer.Write(string.Format(@"COMMIT {0}{1}", DELIMITER, NEW_LINE));
            }
        }
Example #17
0
        public BackupWindowViewModel(BackupOptions options)
        {
            _dispatcher = Dispatcher.CurrentDispatcher;
            _options    = options;

            Task.Run(async() =>
            {
                try
                {
                    IsWorking = true;

                    await Backup();
                }
                catch (Exception e)
                {
                    SentrySdk.CaptureException(e);
                    IsErrored = true;
                }
                finally
                {
                    IsWorking = false;
                }

                if (IsErrored)
                {
                    return;
                }

                _dispatcher.Invoke(() => { Messages.Add(Strings.Done___); });
                _options.LastBackup        = DateTime.Now;
                _options.Provider.FirstRun = false;

                await Task.Delay(1000);

                _dispatcher.Invoke(CloseWindow);
            }).ConfigureAwait(false);
        }
        public async Task Reach_Maximum_Backup_File_Mongo()
        {
            // Arrange
            if (!_context.AllowMongoDB)
            {
                Assert.True(true);
                return;
            }

            BackupOptions backupOptions = new BackupOptions
            {
                BackupFolderPath = "Backup",
                MaximumObjects   = 0
            };

            BackupMongoRepository backupRepository = new BackupMongoRepository(_context.GetMongoConnection());
            IBackupService        backupService    = GetMockBackupService(backupRepository, backupOptions);

            // Act
            try
            {
                LetPortal.Portal.Models.Recoveries.BackupResponseModel result = await backupService.CreateBackupFile(new LetPortal.Portal.Models.Recoveries.BackupRequestModel
                {
                    Name        = "BK_Test",
                    Description = "BK Test",
                    Creator     = "Admin"
                });
            }
            catch (Exception ex)
            {
                Assert.True(ex is BackupException backupException && backupException.ErrorCode.MessageCode == BackupErrorCodes.ReachMaximumBackupObjects.MessageCode);
            }
            finally
            {
                backupRepository.Dispose();
            }
        }
Example #19
0
        static void BackupInteractive(string mountPoint = null)
        {
            if (mountPoint == null)
            {
                Console.Write("Backup location?: ");
                mountPoint = Console.ReadLine().Trim();
            }

            var volumes = new List <BackupOptions>();

            GatherVolumes(mountPoint, volumes);

            // Show menu and prompt for stuff
            while (true)
            {
                var menu = new Table("_", "volume", "incremental", "snapshot date");
                for (int i = 0; i < volumes.Count; i++)
                {
                    menu.Add(
                        (i + 1).ToString(),
                        volumes[i].Filesystem,
                        volumes[i].IncrementalStartDate ?? "full backup",
                        volumes[i].SnapshotDate
                        );
                }

                Console.WriteLine();
                Console.WriteLine("Volumes to backup:");
                menu.Print();
                Console.WriteLine();

                Console.Write("Enter a number to make changes,\n"
                              + "\t'+' to add a volume,\n"
                              + "\t'-' to remove one,\n"
                              + "\t'd' to change all dates,\n"
                              + "\tor <return> to start backup: ");
                string input = Console.ReadLine();

                if (input == "+")
                {
                    Console.Write("Volume: ");
                    input = Console.ReadLine();

                    var opts = new BackupOptions();
                    if (GatherVolume(input, ref opts))
                    {
                        volumes.Add(opts);
                    }
                }
                else if (input.StartsWith("-"))
                {
                    int n;
                    if (!int.TryParse(input.Substring(1), out n))
                    {
                        Console.Write("Remove which one?: ");
                        if (!int.TryParse(Console.ReadLine(), out n))
                        {
                            Console.WriteLine("Invalid number");
                            continue;
                        }
                    }

                    if (n <= 0 || n > volumes.Count)
                    {
                        Console.WriteLine("Index out of range.");
                        continue;
                    }

                    volumes.RemoveAt(n - 1);
                }
                else if (input.StartsWith("d") || input.StartsWith("D"))
                {
                    Console.Write("Snapshot date (yyyy-MM-dd): ");
                    string date = Console.ReadLine();

                    for (int i = 0; i < volumes.Count; i++)
                    {
                        volumes[i].SnapshotDate = date;
                    }
                }
                else if (input == string.Empty)
                {
                    break;
                }
                else
                {
                    int n;
                    if (!int.TryParse(input, out n))
                    {
                        Console.WriteLine("Invalid number.");
                        continue;
                    }

                    if (n <= 0 || volumes.Count < n)
                    {
                        Console.WriteLine("Index out of range.");
                        continue;
                    }

                    var opts = volumes[n - 1];

                    Console.Write("Change (I)ncremental starting snapshot, (S)napshot date: ");
                    input = Console.ReadLine();
                    if (input == "I" || input == "i")
                    {
                        Console.Write("Date (yyyy-MM-dd): ");
                        opts.IncrementalStartDate = Console.ReadLine();
                    }
                    else if (input == "S" || input == "s")
                    {
                        Console.Write("Date (yyyy-MM-dd): ");
                        opts.SnapshotDate = Console.ReadLine();
                    }
                    else
                    {
                        Console.WriteLine("Invalid selection.");
                        continue;
                    }

                    volumes.RemoveAt(n - 1);
                    volumes.Insert(n - 1, opts);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Starting backups.");
            Console.WriteLine();
            DoBackups(volumes, mountPoint);
        }
        private IBackupService GetMockBackupServiceForRestore(IBackupRepository backupRepository,
                                                              BackupOptions options)
        {
            Mock <IAppServiceProvider> mockAppProvider = new Mock <IAppServiceProvider>();

            mockAppProvider
            .Setup(a => a.GetAppsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <App> >(null));

            Mock <IStandardServiceProvider> mockStandardProvider = new Mock <IStandardServiceProvider>();

            mockStandardProvider
            .Setup(a => a.GetStandardComponentsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <StandardComponent> >(null));

            Mock <IChartServiceProvider> mockChartProvider = new Mock <IChartServiceProvider>();

            mockChartProvider
            .Setup(a => a.GetChartsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <Chart> >(null));

            Mock <IDynamicListServiceProvider> mockDynamicListProvider = new Mock <IDynamicListServiceProvider>();

            mockDynamicListProvider
            .Setup(a => a.GetDynamicListsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <DynamicList> >(null));

            Mock <IPageServiceProvider> mockPageProvider = new Mock <IPageServiceProvider>();

            mockPageProvider
            .Setup(a => a.GetPagesByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <Page> >(null));

#pragma warning disable CA2000 // Dispose objects before losing scope
            InternalDatabaseServiceProvider databaserProvider = new InternalDatabaseServiceProvider(
                new DatabaseService(null, null),
                new DatabaseMongoRepository(_context.GetMongoConnection()));
#pragma warning restore CA2000 // Dispose objects before losing scope

            Mock <IFileSeviceProvider> mockFileProvider = new Mock <IFileSeviceProvider>();
            mockFileProvider
            .Setup(a => a.UploadFileAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
            .Returns(Task.FromResult(new ResponseUploadFile {
                DownloadableUrl = "http://localhost", FileId = DataUtil.GenerateUniqueId()
            }));

            mockFileProvider
            .Setup(a => a.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(Task.FromResult(true));

            mockFileProvider
            .Setup(a => a.DownloadFileAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(new ResponseDownloadFile
            {
                FileBytes = File.ReadAllBytes(@"Artifacts\" + zipFileName),
                FileName  = zipFileName,
                MIMEType  = "application/zip"
            }));

            IOptionsMonitor <BackupOptions> backupOptionsMock = Mock.Of <IOptionsMonitor <BackupOptions> >(_ => _.CurrentValue == options);

            Mock <ICompositeControlServiceProvider> mockCompositeControlProvider = new Mock <ICompositeControlServiceProvider>();
            mockCompositeControlProvider
            .Setup(a => a.GetByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <CompositeControl> >(null));

            BackupService backupService = new BackupService(
                mockAppProvider.Object,
                mockStandardProvider.Object,
                mockChartProvider.Object,
                mockDynamicListProvider.Object,
                databaserProvider,
                mockPageProvider.Object,
                mockFileProvider.Object,
                backupRepository,
                mockCompositeControlProvider.Object,
                backupOptionsMock,
                new FakeServiceLogger <BackupService>());

            databaserProvider.Dispose();

            return(backupService);
        }
        private IBackupService GetMockBackupService(
            IBackupRepository backupRepository,
            BackupOptions options)
        {
            Mock <IAppServiceProvider> mockAppProvider = new Mock <IAppServiceProvider>();

            mockAppProvider
            .Setup(a => a.GetAppsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <App> >(null));

            Mock <IStandardServiceProvider> mockStandardProvider = new Mock <IStandardServiceProvider>();

            mockStandardProvider
            .Setup(a => a.GetStandardComponentsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <StandardComponent> >(null));

            Mock <IChartServiceProvider> mockChartProvider = new Mock <IChartServiceProvider>();

            mockChartProvider
            .Setup(a => a.GetChartsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <Chart> >(null));

            Mock <IDynamicListServiceProvider> mockDynamicListProvider = new Mock <IDynamicListServiceProvider>();

            mockDynamicListProvider
            .Setup(a => a.GetDynamicListsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <DynamicList> >(null));

            Mock <IPageServiceProvider> mockPageProvider = new Mock <IPageServiceProvider>();

            mockPageProvider
            .Setup(a => a.GetPagesByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <Page> >(null));

            Mock <IDatabaseServiceProvider> mockDatabaseProvider = new Mock <IDatabaseServiceProvider>();

            mockDatabaseProvider
            .Setup(a => a.GetDatabaseConnectionsByIds(It.IsAny <IEnumerable <string> >()))
            .Returns(Task.FromResult <IEnumerable <DatabaseConnection> >(new List <DatabaseConnection> {
                _context.MongoDatabaseConenction
            }));

            mockDatabaseProvider
            .Setup(a => a.CompareDatabases(It.IsAny <IEnumerable <DatabaseConnection> >()))
            .Returns(Task.FromResult <IEnumerable <ComparisonResult> >(new List <ComparisonResult> {
            }));

            Mock <IFileSeviceProvider> mockFileProvider = new Mock <IFileSeviceProvider>();

            mockFileProvider
            .Setup(a => a.UploadFileAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>()))
            .Returns(Task.FromResult(new ResponseUploadFile {
                DownloadableUrl = "http://localhost", FileId = DataUtil.GenerateUniqueId()
            }));

            mockFileProvider
            .Setup(a => a.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(Task.FromResult(true));

            mockFileProvider
            .Setup(a => a.DownloadFileAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(new ResponseDownloadFile
            {
                FileBytes = File.ReadAllBytes(@"Artifacts\" + zipFileName),
                FileName  = zipFileName,
                MIMEType  = "application/zip"
            }));

            IOptionsMonitor <BackupOptions> backupOptionsMock = Mock.Of <IOptionsMonitor <BackupOptions> >(_ => _.CurrentValue == options);

            BackupService backupService = new BackupService(
                mockAppProvider.Object,
                mockStandardProvider.Object,
                mockChartProvider.Object,
                mockDynamicListProvider.Object,
                mockDatabaseProvider.Object,
                mockPageProvider.Object,
                mockFileProvider.Object,
                backupRepository,
                backupOptionsMock);

            return(backupService);
        }
Example #22
0
        static private void ExportTableData(MySqlConnector conn, StreamWriter writer, BackupOptions options)
        {
            List<string> lstTables = GetObjectList(conn, DbObjectType.TABLE);

            foreach (string table in lstTables)
            {
                if (IsView(conn, table))
                {
                    continue;
                }

                Query query = options.GetTableDataQuery(table);

                using (DataReaderBase reader = (query == null ? conn.ExecuteReader(string.Format(@"SELECT * FROM {0}", table)) : query.ExecuteReader(conn)))
                {
                    while (reader.Read())
                    {
                        writer.Write(string.Format(@"INSERT INTO {0} (", table));
                        for (Int32 idx = 0, count = reader.GetColumnCount(); idx < count; idx++)
                        {
                            if (idx > 0) writer.Write(@",");
                            writer.Write(conn.EncloseFieldName(reader.GetColumnName(idx)));
                        }
                        writer.Write(@") VALUES(");
                        for (Int32 idx = 0, count = reader.GetColumnCount(); idx < count; idx++)
                        {
                            if (idx > 0) writer.Write(@",");
                            writer.Write(conn.PrepareValue(reader[idx]));
                        }
                        writer.Write(string.Format(@") {0}{1}", DELIMITER, NEW_LINE));
                    }
                }
            }
        }
Example #23
0
 public BackupService(BackupOptions options)
 {
     Options = options;
 }
 public RetentionTask(IStorageProvider storageProvider, RetentionOptions retention, BackupOptions backupOptions)
 {
     this.storageProvider = storageProvider;
     this.retention       = retention;
     this.backupOptions   = backupOptions;
 }
Example #25
0
        //start the backup process
        public List<BackupLog> StartBackup(BackupOptions deleteOption)
        {
            logs = new List<BackupLog>() ; //create object to keep the results

            //start copying all new/changed files from origin to destination
            CopyNewFiles(originRoot, destRoot, null) ;

            //if won't cumulatively backup, clean files from destination, that not present in origin
            if(deleteOption == BackupOptions.DeleteFilesNotInOrigin){
                CleanDeletedFiles(destRoot, originRoot, null) ;
            }

            return logs ;
        }
Example #26
0
        static void GatherVolumes(string mountPoint, List <BackupOptions> volumes)
        {
            IEnumerable <string> files = null;

            try
            {
                files = Directory.EnumerateFiles(mountPoint, "*.zfs*", System.IO.SearchOption.TopDirectoryOnly);
            }
            catch (DirectoryNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (files.Count() == 0)
            {
                return;
            }

            string snapList = string.Join("\n",
                                          files.Where(s => !s.EndsWith("_partial"))
                                          .Select(s => new string(
                                                      s.Take(s.LastIndexOf(".zfs"))
                                                      .Skip(s.LastIndexOf(Path.DirectorySeparatorChar) + 1)
                                                      .ToArray())));

            ZfsSnapshots backups = new ZfsSnapshots(snapList);
            ZfsSnapshots snaps   = new ZfsSnapshots();

            var namesToFilesystems = new Dictionary <string, string>();

            foreach (string name in backups.Filesystems())
            {
                if (snaps.Filesystems().Contains(name.Replace('_', '/')))
                {
                    namesToFilesystems.Add(name, name.Replace('_', '/'));
                }
                else
                {
                    IEnumerable <string> matches = snaps.Filesystems().Where(s => s.EndsWith("/" + name));
                    if (matches.Count() == 1)
                    {
                        namesToFilesystems.Add(name, matches.Single());
                    }
                    else
                    {
                        Console.WriteLine("Backup filename \"{0}\" is ambiguous. Not sure what volume it's for. Skipping it.", name);
                    }
                }
            }

            foreach (KeyValuePair <string, string> pair in namesToFilesystems)
            {
                var opts = new BackupOptions();

                opts.Filename   = pair.Key;
                opts.Filesystem = pair.Value;

                var newestBackup   = backups.Snapshots(pair.Key).OrderByDescending(s => s.Date).First();
                var newestSnapshot = snaps.Snapshots(pair.Value).OrderByDescending(s => s.Date).First();

                opts.SnapshotDate = newestSnapshot.Date.ToString("yyyy-MM-dd");

                try
                {
                    ZfsSnapshots.SnapInfo snapForBackup = snaps.Snapshots(pair.Value).Where(s => s.Date == newestBackup.Date).Single();

                    if (newestSnapshot.Date == snapForBackup.Date)
                    {
                        Console.WriteLine("Backup file \"{0}\" is already current.", newestBackup.Name);
                        opts.SnapshotDate = null;
                    }
                    else
                    {
                        opts.IncrementalStartDate = snapForBackup.Date.ToString("yyyy-MM-dd");
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Couldn't find a snapshot for backup file \"{0}\". Doing non-incremental backup.", newestBackup.Name);
                    opts.IncrementalStartDate = null;
                }

                volumes.Add(opts);
            }
        }
        public static void Save()
        {
            if (BackupOptions != null && BackupOptions.Enabled &&
                DateTime.Now - BackupOptions.LastBackup >= TimeSpan.FromDays(BackupOptions.Days))
            {
                BackupProfiles();
            }

            JObject json = new JObject
            {
                { "LanguageOverride", LanguageOverride.ToString() },
                { "LastProfile", LastProfile },
                { "UpdateGumpVersion", UpdateGumpVersion?.ToString() ?? "0.0.0.0" },
                { "SavePasswords", SavePasswords },
                { "SavePasswordsOnlyBlank", SavePasswordsOnlyBlank },
                { "UserId", UserId },
                { "UseCUOClilocLanguage", UseCUOClilocLanguage },
#if !DEVELOP
                { "WindowWidth", WindowWidth },
                { "WindowHeight", WindowHeight },
#endif
            };

            BackupOptions?.Serialize(json);

            JArray linkedProfilesArray = new JArray();

            foreach (JObject linkedObj in _linkedProfiles.Select(profile =>
                                                                 new JObject {
                { "Serial", profile.Key }, { "Profile", profile.Value }
            }))
            {
                linkedProfilesArray.Add(linkedObj);
            }

            json.Add("Profiles", linkedProfilesArray);

            JArray savedPasswordsArray = new JArray();

            foreach (KeyValuePair <string, string> kvp in SavedPasswords)
            {
                savedPasswordsArray.Add(new JObject
                {
                    { "Username", kvp.Key }, { "Password", Crypter.Encrypt(kvp.Value) }
                });
            }

            json.Add("SavedPasswords", savedPasswordsArray);

            JArray assembliesArray = new JArray();

            foreach (string assembly in Assemblies ?? new string[0])
            {
                assembliesArray.Add(assembly);
            }

            json.Add("Assemblies", assembliesArray);

            File.WriteAllText(Path.Combine(Engine.StartupPath ?? Environment.CurrentDirectory, "Assistant.json"),
                              json.ToString(Formatting.Indented));
        }
        public static void Load()
        {
            Engine.PlayerInitializedEvent += PlayerInitialized;

            string configPath = Path.Combine(Engine.StartupPath ?? Environment.CurrentDirectory, "Assistant.json");

            if (!File.Exists(configPath))
            {
                LastProfile   = Options.DEFAULT_SETTINGS_FILENAME;
                UserId        = Guid.NewGuid().ToString();
                SessionId     = Guid.NewGuid().ToString();
                BackupOptions = new BackupOptions();
                return;
            }

            JObject json = JObject.Parse(File.ReadAllText(configPath));

            LanguageOverride  = json["LanguageOverride"]?.ToObject <Language>() ?? Language.Default;
            LastProfile       = json["LastProfile"]?.ToObject <string>() ?? Options.DEFAULT_SETTINGS_FILENAME;
            UpdateGumpVersion = json["UpdateGumpVersion"]?.ToObject <string>() ?? new Version().ToString();

            if (json["Backup"] == null)
            {
                BackupOptions = new BackupOptions
                {
                    Enabled    = !IsTesting() /*json["AutoBackupProfiles"]?.ToObject<bool>() ?? true*/,
                    Days       = json["AutoBackupProfilesDays"]?.ToObject <int>() ?? 7,
                    LastBackup = json["AutoBackupProfilesLast"]?.ToObject <DateTime>() ?? default,
                    Provider   = new LocalBackupProvider
                    {
                        BackupPath = json["AutoBackupProfilesDirectory"]?.ToObject <string>() ??
                                     BackupOptions.DefaultBackupPath
                    }
                };
            }
            else
            {
                BackupOptions = new BackupOptions();
                BackupOptions.Deserialize(json, Options.CurrentOptions);
            }

            SavePasswords          = json["SavePasswords"]?.ToObject <bool>() ?? false;
            SavePasswordsOnlyBlank = json["SavePasswordsOnlyBlank"]?.ToObject <bool>() ?? false;
            UserId               = json["UserId"]?.ToObject <string>() ?? Guid.NewGuid().ToString();
            WindowWidth          = json["WindowWidth"]?.ToObject <int>() ?? 625;
            WindowHeight         = json["WindowHeight"]?.ToObject <int>() ?? 500;
            UseCUOClilocLanguage = json["UseCUOClilocLanguage"]?.ToObject <bool>() ?? false;
            Assemblies           = json["Assemblies"]?.ToObject <string[]>() ?? new string[0];
            SessionId            = Guid.NewGuid().ToString();

            if (json["Profiles"] != null)
            {
                foreach (JToken token in json["Profiles"])
                {
                    _linkedProfiles.Add(token["Serial"]?.ToObject <int>() ?? 0, token["Profile"]?.ToObject <string>());
                }
            }

            if (json["SavedPasswords"] != null)
            {
                foreach (JToken token in json["SavedPasswords"])
                {
                    SavedPasswords.Add(token["Username"]?.ToObject <string>() ?? string.Empty,
                                       Crypter.Decrypt(token["Password"]?.ToObject <string>()));
                }

                OnPasswordsChanged();
            }

            SetLanguage(LanguageOverride);

            foreach (string assembly in Assemblies)
            {
                try
                {
                    Assembly asm = Assembly.LoadFile(assembly);

                    IEnumerable <MethodInfo> initializeMethods = asm.GetTypes()
                                                                 .Where(e => e.IsClass && e.IsPublic && e.GetMethod("Initialize",
                                                                                                                    BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null) != null)
                                                                 .Select(e => e.GetMethod("Initialize", BindingFlags.Public | BindingFlags.Static, null,
                                                                                          Type.EmptyTypes, null));

                    foreach (MethodInfo initializeMethod in initializeMethods)
                    {
                        initializeMethod?.Invoke(null, null);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            OptionsLoaded?.Invoke(null, EventArgs.Empty);
        }