LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2008, 07:09 AM   #1
#cousin#
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Rep: Reputation: 0
Question Bash script for database backup


Hi,

I need a bash script wich connects from my debian server to another debain server via ssh. Changes there to root and executes a pg_dump command. Afterwards the script should copie the file via scp back on my debian server where it schould be renamed with the actual date.

I know the single commands but I don't know how to get it in a bash script. (especially the ssh and root password)

Can anyone help me with this problem??

Many thanks!!!!
 
Old 07-09-2008, 12:50 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,449

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi

First you need to setup key based ssh authentication. From the first server, run the command
"ssh-keygen -t rsa" as root. Hit enter when it asks you for a passphrase. It will generate two files in "/root/.ssh". Copy "/root/.ssh/id_rsa.pub" over to the other server, and rename it to "/root/.ssh/authorized_keys". Then verify that you can ssh to the server without typing the password.

Then, you can run commands on the other server by passing them as options to ssh. The output of the commands can be piped to files, and those files will be stored on the server you run the ssh command on.

backupfile=$(date +%Y%m%d).sql
ssh otherserver.example.org 'pg_dump --options...' >$backupfile
 
Old 07-09-2008, 06:09 PM   #3
#cousin#
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
thanks a lot!

everything works fine except the password. I still have to type it. What could be the problem?
 
Old 07-10-2008, 04:24 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,449

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi again.

Here's a better guide on how to set it up:
http://www.debian-administration.org/articles/152
 
Old 07-28-2008, 10:02 AM   #5
#cousin#
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
thanks...now everything works as I want
 
Old 08-27-2008, 05:20 PM   #6
#cousin#
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Hi again,

I have a little Problem with this script again. I inserted the commands for openVPNc and it works fine when I run it manually. But if I want to run the script through a cronjob it creates the file but without any content. I only get the following error:

Code:
vpnc-connect: command not found
I don`t understand this error, because the script works fine, when I manually run it from the same user.


Can anyone help me?


Code:
#!/bin/bash
#
# this script is for database backup
#

# start VPN
vpnc-connect /etc/vpnc/nslu2_2.conf

# backup process
backupfile=/home/m/backup/printerdb-$(date +%d-%m-%Y).backup
ssh user@server pg_dump printerdb >$backupfile

# cleanup process
find /home/m/backup/ -mtime +21 -exec rm -r {} \;

# stop VPN
vpnc-disconnect
 
Old 08-28-2008, 02:27 AM   #7
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Make sure you include full path to your binaries such as vpnc-connect, find, and vpnc-disconnect in your script when running it as a cronjob as cron env is very limited.

Last edited by kenoshi; 08-28-2008 at 02:31 AM.
 
Old 08-29-2008, 03:10 AM   #8
#cousin#
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
When I change the lines in the script so that there are the full paths to the commands I get the following error:

Code:
etc/vpnc/vpnc-script: line 97: ifconfig: command not found
RTNETLINK answers: No such device


Code:
# start VPN
/usr/sbin/vpnc-connect nslu2_2.conf
or isn't this the right path??
 
Old 08-29-2008, 06:00 AM   #9
#cousin#
LQ Newbie
 
Registered: Jul 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Ok, now I got it......it works fine and it was realy a missing $PATH in my crontab. With the following lines included it works.

Code:
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
SHELL=/bin/sh
 
  


Reply

Tags
backup, database, ssh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
backup script for database maooah Linux - Enterprise 9 06-15-2008 09:58 AM
bash backup script kristof_v Programming 4 07-24-2007 07:33 PM
Cannot running export database using bash script tovohery Linux - General 2 03-22-2007 06:59 AM
bash script for database searching using crontab saurya_s Linux - Software 5 01-22-2004 08:53 AM
Need script to dump database, ftp file to backup server glock19 Linux - Networking 17 05-12-2003 05:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration