Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I support a company with a remotely hosted web site. They use PostgreSQL dbs.

I can't write scripts on the remote server, and I don't want to store any backup files on the remote server, but I can run cron jobs, and I have access to the remote server's SSH public key. I set up a local linux host to accept the remote host's private key for authentication.

I need to make a one-line script to backup, compress, and scp the backup file to my local linux box synchronously.

If my local linux box was reachable via SSH at mylinuxbox.foo.com on port 122, and my database's name was mydatabase, what would this one-liner cron job command look like?

share|improve this question
    
Can you access the database remotely? If so might be easier to write a script on the local box to pull the data instead of trying to push it from the remote side. –  Grant Dec 6 '13 at 13:31
    
There are some provisions for shell access, but I'd rather it be a push operation if I can swing it. –  Jeremy Holovacs Dec 6 '13 at 15:43
    
Have you read the section of the Postgres manual dealing with backup and restore? If not, stop now and go read it. Then all you need to do is wrap those instruction sin a shell script and run it from cron. –  voretaq7 Dec 6 '13 at 16:00

1 Answer 1

up vote 3 down vote accepted

Assuming you're using ident authentication (or a .pgpass) for the database I'd probably do something like the following :

pg_dump -Udbuser -h127.0.0.1 mydatabase |gzip -c > mydatabase.backup.gz; scp -P 122 mydatabase.backup.gz user@mylinuxbox.foo.com:; rm -f mydatabase.backup.gz

Editing to add another option...

If it's really important to not have intermediate storage of the backup file you could modify the above to look more like this:

pg_dump -Udbuser -h127.0.0.1 mydatabase |gzip -c | ssh -p 122 user@mylinuxbox.foo.com "cat >mydatabase.backup.gz"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.