Exemple #1
0
        public void BackupDatabase(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                this.Clients.Caller.backupFailed(Warnings.NoFileSpecified);
                return;
            }

            PostgreSQLServer server = new PostgreSQLServer
            {
                BinDirectory            = ConfigurationHelper.GetDbServerParameter("PostgreSQLBinDirectory"),
                DatabaseBackupDirectory = ConfigurationHelper.GetDbServerParameter("DatabaseBackupDirectory"),
                DatabaseName            = AppUsers.GetCurrentUserDB(),
                HostName   = ConfigurationHelper.GetDbServerParameter("Server"),
                PortNumber = Conversion.TryCastInteger(ConfigurationHelper.GetDbServerParameter("Port")),
                UserId     = ConfigurationHelper.GetDbServerParameter("UserId"),
                Password   = ConfigurationHelper.GetDbServerParameter("Password")
            };

            server.Validate();

            if (server.IsValid && !string.IsNullOrWhiteSpace(server.BinDirectory) &&
                !string.IsNullOrWhiteSpace(server.DatabaseBackupDirectory))
            {
                this.Backup(server, fileName);
                return;
            }

            this.Clients.Caller.backupFailed(Warnings.ConfigurationError);
        }
Exemple #2
0
        private bool BackupDatabase(string pgDumpPath, PostgreSQLServer server, string fileName)
        {
            this.CreateBatchFile(server, pgDumpPath, fileName);

            using (Process process = new Process())
            {
                process.StartInfo.FileName = batchFile;

                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.ErrorDialog            = false;
                process.StartInfo.RedirectStandardInput  = true;
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;

                process.ErrorDataReceived  += this.Data_Received;
                process.OutputDataReceived += this.Data_Received;
                process.Disposed           += this.Completed;

                process.Start();

                process.BeginErrorReadLine();
                process.BeginOutputReadLine();

                process.WaitForExit();


                return(true);
            }
        }
Exemple #3
0
        private void Backup(PostgreSQLServer server, string fileName)
        {
            string pgdumpPath      = Path.Combine(server.BinDirectory, "pg_dump.exe");
            string backupDirectory = HostingEnvironment.MapPath(server.DatabaseBackupDirectory);

            if (backupDirectory != null)
            {
                string path   = Path.Combine(backupDirectory, fileName + ".backup");
                var    result = this.BackupDatabase(pgdumpPath, server, path);

                if (result)
                {
                    StringBuilder message = new StringBuilder();
                    message.Append(Labels.DatabaseBackupSuccessful);
                    message.Append(" ");
                    message.Append("<a href='");
                    message.Append(PageUtility.ResolveUrl(Path.Combine(server.DatabaseBackupDirectory, fileName + ".backup")));
                    message.Append("'");
                    message.Append(" target='_blank'>");
                    message.Append(Labels.ClickHereToDownload);
                    message.Append("</a>");

                    this.Clients.Caller.backupCompleted(message.ToString());
                    return;
                }

                this.Clients.Caller.backupFailed(Warnings.CannotCreateABackup);
            }
        }
Exemple #4
0
        public void BackupDatabase(string fileName)
        {
            if (!this.IsValidRequest())
            {
                this.Clients.Caller.getNotification(Warnings.AccessIsDenied, Warnings.AccessIsDenied);
                return;
            }

            if (string.IsNullOrWhiteSpace(fileName))
            {
                this.Clients.Caller.backupFailed(Warnings.NoFileSpecified);
                return;
            }

            PostgreSQLServer server = new PostgreSQLServer();

            server.DatabaseName = AppUsers.GetCurrentUserDB();

            BackupAgent agent = new BackupAgent(server, fileName);

            agent.BackupFail += delegate(BackupProgressInfo info)
            {
                this.Clients.Caller.backupFailed(info.Message);
            };

            agent.Progress += delegate(BackupProgressInfo info)
            {
                this.Clients.Caller.getNotification(info.Message);
            };

            agent.BackupComplete += delegate
            {
                StringBuilder message = new StringBuilder();
                message.Append(Labels.DatabaseBackupSuccessful);
                message.Append("&nbsp;");
                message.Append("<a href='");
                message.Append(
                    PageUtility.ResolveUrl(Path.Combine(server.DatabaseBackupDirectory, fileName + ".zip")));
                message.Append("'");
                message.Append(" target='_blank'>");
                message.Append(Labels.ClickHereToDownload);
                message.Append("</a>");

                this.Clients.Caller.backupCompleted(message.ToString());
            };

            try
            {
                agent.PerformBackup();
            }
            catch (Exception ex)
            {
                this.Clients.Caller.backupFailed(ex.Message);
            }
        }
Exemple #5
0
        private string CreateBatchFile(PostgreSQLServer server, string pgDumpPath, string fileName)
        {
            Collection <string> commands = new Collection <string>();

            commands.Add("@echo off");
            commands.Add("SET PGPASSWORD="******"""{0}"" --host ""{1}"" --port {2} --username ""{3}"" --format custom --blobs --verbose --file ""{4}"" ""{5}""";

            command = string.Format(CultureInfo.InvariantCulture, command, pgDumpPath, server.HostName, server.PortNumber, server.UserId, fileName, server.DatabaseName);
            commands.Add(command);
            commands.Add("exit");

            string batchFilePath = fileName + ".bat";

            File.WriteAllText(batchFilePath, string.Join(Environment.NewLine, commands));
            return(batchFilePath);
        }
Exemple #6
0
        public void BackupDatabase(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                this.Clients.Caller.backupFailed(Warnings.NoFileSpecified);
                return;
            }

            PostgreSQLServer server = new PostgreSQLServer();

            server.Validate();

            if (server.IsValid && !string.IsNullOrWhiteSpace(server.BinDirectory) && !string.IsNullOrWhiteSpace(server.DatabaseBackupDirectory))
            {
                this.Backup(server, fileName);
                return;
            }

            this.Clients.Caller.backupFailed(Warnings.ConfigurationError);
        }