LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script for database backup (https://www.linuxquestions.org/questions/programming-9/bash-script-for-database-backup-654570/)

#cousin# 07-09-2008 07:09 AM

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!!!!

Guttorm 07-09-2008 12:50 PM

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

#cousin# 07-09-2008 06:09 PM

thanks a lot!

everything works fine except the password. I still have to type it. What could be the problem?

Guttorm 07-10-2008 04:24 AM

Hi again.

Here's a better guide on how to set it up:
http://www.debian-administration.org/articles/152

#cousin# 07-28-2008 10:02 AM

thanks...now everything works as I want

#cousin# 08-27-2008 05:20 PM

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


kenoshi 08-28-2008 02:27 AM

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.

#cousin# 08-29-2008 03:10 AM

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??

#cousin# 08-29-2008 06:00 AM

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



All times are GMT -5. The time now is 07:16 AM.