Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Backup/Restore PostgreSQL databases

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
5 Mar 2013CPOL1 min read 33.1K   4.5K   10  
Allow your applications the ability to backup and restore your PostgreSQL databases.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using pgstore;
namespace StoreApp
{
    public partial class frmBackup : Form
    {
        #region constructor
        public frmBackup()
        {
            InitializeComponent();
        }
        #endregion

        #region private method
        void SetConf()
        {
            pgstore.Config cnf = new Config();
            cnf.ServerName = txtHost.Text;
            if (lstDB.Items.Count > 0 && lstDB.SelectedItem!=null)
                cnf.DataBase = lstDB.SelectedItem.ToString();
            cnf.UserName = txtUserName.Text;
            cnf.Password = txtPass.Text;
            cnf.Port = Convert.ToInt16(txtPort.Text);
            pgstore.Control.CurrentConfig = cnf;
        }
        #endregion

        #region events
        private void cmdCreateBackup_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            lblInfo.Text = "";
            try
            {
                SetConf();
                FolderBrowserDialog dlgFolder = new FolderBrowserDialog();
                dlgFolder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string FileName = pgstore.Control.CurrentConfig.DataBase + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + ".backup";

                if (dlgFolder.ShowDialog() != DialogResult.Cancel)
                {
                    pgstore.Control.ResultChanged += Control_ResultChanged;
                    lblInfo.Text = "Crating backup ...";
                    pgstore.Control.Backup(dlgFolder.SelectedPath, FileName, true);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void cmdRestore_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            lblInfo.Text = "";
            if (MessageBox.Show("Cette opération est irréversible, êtes vous sur de bien vouloir procéder ?", "Restauration", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No) return;
            try
            {
                SetConf();
                OpenFileDialog myOpenFileDialog = new OpenFileDialog();
                myOpenFileDialog.Filter = "Backup files (*.backup)|*.backup";

                if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
                {
                    lblInfo.Text = "Restauration en cours .....";
                    string path = myOpenFileDialog.FileName;
                    pgstore.Control.ResultChanged += Control_ResultChanged;
                    pgstore.Control.Restaure(path, chkReplace.Checked);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        private void cmdConnect_Click(object sender, EventArgs e)
        {
            try
            {
                SetConf();
                lstDB.Items.Clear();
                foreach (string item in pgstore.Control.GetDataBases())
                {
                    lstDB.Items.Add(item);
                }
                cmdConnect.Enabled = false;
                cmdDisconnect.Enabled = true;
                txtHost.Enabled = false;
                txtPort.Enabled = false;
                txtUserName.Enabled = false;
                txtPass.Enabled = false;
                cmdCreateBackup.Enabled = true;
                cmdRestaure.Enabled = true;


            }
            catch (Exception ex)
            {

                lblInfo.Text = ex.Message;
            }

        }
        private void cmdDisconnect_Click(object sender, EventArgs e)
        {
            lstDB.Items.Clear();
            SetConf();
            cmdConnect.Enabled = true;
            cmdDisconnect.Enabled = false;
            txtHost.Enabled = true;
            txtPort.Enabled = true;
            txtUserName.Enabled = true;
            txtPass.Enabled = true;
            cmdCreateBackup.Enabled = false;
            cmdRestaure.Enabled = false;

        }
        private void lstDB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (pgstore.Control.CurrentConfig == null) this.SetConf();
            if (lstDB.SelectedItem != null)
                pgstore.Control.CurrentConfig.DataBase = lstDB.SelectedItem.ToString();
        }
        void Control_ResultChanged(object sender, EventArgs e)
        {
            lblInfo.Text += sender.ToString();
            lblInfo.SelectionStart = lblInfo.Text.Length;
            lblInfo.ScrollToCaret();
            lblInfo.Refresh();
        }
#endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) B&B
Tunisia Tunisia
Microsoft DOTNET developer
VOIP Technologies
ERP Petales

http://www.declaration.tn/

Comments and Discussions