Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Backup/Restore PostgreSQL databases

, 5 Mar 2013
Allow your applications the ability to backup and restore your PostgreSQL databases.
pgstore-noexe.zip
pgstore
Properties
pgstore.zip
bin
Debug
pgstore.dll
Release
lib
libeay32.dll
libiconv-2.dll
libintl-8.dll
libpq.dll
Npgsql.dll
pg_dump.exe
pg_restore.exe
ssleay32.dll
zlib1.dll
obj
Debug
TempPE
StoreApp-noexe.zip
StoreApp
bin
Debug
Properties
Settings.settings
Resources
backup-review.png
StoreApp.zip
pgstore.dll
StoreApp.exe
StoreApp.vshost.exe
Release
obj
Debug
TempPE
x86
Debug
TempPE
Settings.settings
backup-review.png
pgstore.dll
libeay32.dll
libiconv-2.dll
libintl-8.dll
libpq.dll
Npgsql.dll
pg_dump.exe
pg_restore.exe
ssleay32.dll
zlib1.dll
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)

About the Author

Saddoud Anis
Software Developer (Senior) B&B
Tunisia Tunisia
Microsoft DOTNET developer
VOIP Technologies
ERP Petales
 
http://www.declaration.tn/

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 5 Mar 2013
Article Copyright 2013 by Saddoud Anis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid