Find the answer to your Linux question:
Results 1 to 4 of 4
I am trying to bacj up files from one host to another using a bash script. I have set up ssh keygen so I don't have to use passwords. At ...
  1. #1
    Just Joined!
    Join Date
    Sep 2006
    Posts
    5

    trouble using ssh in scripts

    I am trying to bacj up files from one host to another using a bash script. I have set up ssh keygen so I don't have to use passwords. At first I just set up 2 scp commands to copy the directories to the backup host. That worked the first time, but it appears that scp will not work when it is run after that. It does not overlay the directories created by the first run.

    So I decided to ssh to the backup machine and use rm to get rid of the directories each time I run the script. Unfortunately, it looks like the ssh command works and I get a shell in the remote host, but then subsequent commands don't run. I have attached the script below, any help would be appreciated. The distro for the host that the script is running on is Debian.

    Mike

    #!/bin/sh
    ssh xxx@yyy.net
    rm -r -f /user/filea
    rm -r -f /user/fileb
    exit
    scp -r /var/lib/filea xxx@yyy.net:/user/filea
    scp -r /var/fileb xxx@yyy.net:/user/fileb

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    I think you want to do it like this

    Code:
    #!/bin/sh
    ssh xxx@yyy.net "rm -r -f /user/filea && rm -r -f /user/fileb"
    scp -r /var/lib/filea xxx@yyy.net:/user/filea
    scp -r /var/fileb xxx@yyy.net:/user/fileb
    The bit in the quotes is the command to be executed remotely. The quotes aren't strictly required, but I used them to get both rm commands to run through a single ssh session.

    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Code:
    "rm -rf /user/file[ab]"
    would do both directories with a single command.

    And if you take the "exit" out of the script you've listed the scp commands will run.

  4. #4
    Just Joined!
    Join Date
    Sep 2006
    Posts
    5
    I used Chris's suggestion and it is working now.Thanks, guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...