Backing up through a database connection (not pg_dump)

Started by Tim Uckunabout 14 years ago9 messagesgeneral
Jump to latest
#1Tim Uckun
timuckun@gmail.com

Is there a way to backup a database or a cluster though a database
connection? I mean I want to write some code that connects to the
database remotely and then issues a backup command like it would issue
any other SQL command. I realize the backups would need to reside on
the database server.

#2John R Pierce
pierce@hogranch.com
In reply to: Tim Uckun (#1)
Re: Backing up through a database connection (not pg_dump)

On 03/26/12 4:05 PM, Tim Uckun wrote:

Is there a way to backup a database or a cluster though a database
connection? I mean I want to write some code that connects to the
database remotely and then issues a backup command like it would issue
any other SQL command. I realize the backups would need to reside on
the database server.

there is no backup command in postgres SQL, you could enumerate the
tables, and use /COPY tablename TO filepath;/ on each table, these files
would have to be in a directory writable by the postgres server process.

alternately, you could open a shell session on the dbserver and run
pg_dump there. frankly, this would be preferable.

--
john r pierce N 37, W 122
santa cruz ca mid-left coast

#3David Boreham
david_list@boreham.org
In reply to: John R Pierce (#2)
Re: Backing up through a database connection (not pg_dump)

fwiw we run db_dump locally, compress the resulting file and scp or
rsync it to the remote server.

#4Tim Uckun
timuckun@gmail.com
In reply to: David Boreham (#3)
Re: Backing up through a database connection (not pg_dump)

On Tue, Mar 27, 2012 at 1:00 PM, David Boreham <david_list@boreham.org> wrote:

fwiw we run db_dump locally, compress the resulting file and scp or rsync it
to the remote server.

I wanted to see if I can do that without running pg_dump on the remote
server. That would involve connecting to the server via ssh and I want
to see if there is a way to avoid that.

Cheers.

Show quoted text

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Stephen Frost
sfrost@snowman.net
In reply to: Tim Uckun (#4)
Re: Backing up through a database connection (not pg_dump)

* Tim Uckun (timuckun@gmail.com) wrote:

On Tue, Mar 27, 2012 at 1:00 PM, David Boreham <david_list@boreham.org> wrote:

fwiw we run db_dump locally, compress the resulting file and scp or rsync it
to the remote server.

I wanted to see if I can do that without running pg_dump on the remote
server. That would involve connecting to the server via ssh and I want
to see if there is a way to avoid that.

Well, sure, run pg_dump on the local system and have it connect to the
remote server.. The result would end up on the local system, of course.

Thanks,

Stephen

#6Bret Stern
bret_stern@machinemanagement.com
In reply to: John R Pierce (#2)
Re: Backing up through a database connection (not pg_dump)

On Mon, 2012-03-26 at 16:16 -0700, John R Pierce wrote:

On 03/26/12 4:05 PM, Tim Uckun wrote:

Is there a way to backup a database or a cluster though a database
connection? I mean I want to write some code that connects to the
database remotely and then issues a backup command like it would issue
any other SQL command. I realize the backups would need to reside on
the database server.

there is no backup command in postgres SQL, you could enumerate the
tables, and use /COPY tablename TO filepath;/ on each table, these files
would have to be in a directory writable by the postgres server process.

alternately, you could open a shell session on the dbserver and run
pg_dump there. frankly, this would be preferable.

--
john r pierce N 37, W 122
santa cruz ca mid-left coast

pg_dump includes more than just the tables though..doesn't it. Meaning,
pg_dump includes stored procedures, views table structure etc?

COPY won't serve as a backup replacement.

I'd go with John's recommendation of pg_dump

Also, pg_dump can write to remote servers (windows example below)

We're also using libpq to trigger backups using NOTIFY from a client
app.

Here's a .bat file I run on a Windows 2003 box

rem for windows date stamped archive

@echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%

set BACKUP_FILE=SKYLINE_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=your_password
echo on
C:\"Program Files"\"PostgreSQL"\8.3\bin\pg_dump -i -h batchfarm -p
5432 -U postgres -F c -b -v -f %BACKUP_FILE% SKYLINE
C:\"Program Files"\"PostgreSQL"\8.3\bin\pg_dump -i -h batchfarm -p
5432 -U postgres -F c -b -v -f \\Acrm-backup\data\Library\Backup\Skyline
\BackupDB\%BACKUP_FILE% SKYLINE

SET PGPASSWORD="

#7Tim Uckun
timuckun@gmail.com
In reply to: Bret Stern (#6)
Re: Backing up through a database connection (not pg_dump)

We're also using libpq to trigger backups using NOTIFY from a client
app.

Do you have an example of how this is done?

#8Eduardo Morras
nec556@retena.com
In reply to: Tim Uckun (#1)
Re: Backing up through a database connection (not pg_dump)

At 01:05 27/03/2012, Tim Uckun wrote:

Is there a way to backup a database or a cluster though a database
connection? I mean I want to write some code that connects to the
database remotely and then issues a backup command like it would issue
any other SQL command. I realize the backups would need to reside on
the database server.

You can use a stored procedure with this pl
http://plsh.projects.postgresql.org/ , like this:

CREATE FUNCTION dump_db(text, text) RETURNS text AS '
#!/bin/sh
pg_dump $1 > $2
' LANGUAGE plsh;

Note that you must CREATE LANGUAGE first, $1 is db_name, $2 is file
name and check for write permissions of $2. Be careful and check your
security because any shell script will run as postgres user. Don't
know if it works with 9.x+ versions of postgres.

HTH

In reply to: Tim Uckun (#1)
Re: Backing up through a database connection (not pg_dump)

On Tue, Mar 27, 2012 at 12:05:00PM +1300, Tim Uckun wrote:

Is there a way to backup a database or a cluster though a database
connection? I mean I want to write some code that connects to the
database remotely and then issues a backup command like it would issue
any other SQL command. I realize the backups would need to reside on
the database server.

Like this:
http://www.depesz.com/2011/01/24/waiting-for-9-1-pg_basebackup/ ?

Best regards,

depesz

--
The best thing about modern society is how easy it is to avoid contact with it.
http://depesz.com/