0

I have a script that uses scp to backup some files of my PC into another PC connected in LAN, something like that:

#!/bin/bash
scp some_directories host@ip-address:backupFolder/

I use a public Key so it works without insert any password, I have just to execute it.

The problem is that if I backup some file from my PC and then I remove it (from my PC not from the remote one), when I execute this script again it copy the new files and overwrites the existing ones but does not remove the previous backuped files that are no longer present in my PC.

So I need a way to remove files in the remote PC via some script. The only way I know is to login in with ssh host@ip and then to use rm but clearly I can't write a script like this:

#!/bin/bash
ssh host@ip-address
rm -r backupFolder/

because in this way the rm command look for the backupFolder in my PC and not in the remote one.

Red
  • 849
  • 3
  • 9
  • 20

3 Answers3

0

You description is a bit vague. BUt I reckon what you want to archive is to sync the remote backup folder with your PC (sync directories).

In this case, the most suitable tool would be rsync instead of scp.

Good news is that rsync works perfectly fine over SSH, your public key authentication will keep working like a charm.

You can use command like below to keep your backup on the remote server in sync with your PC

rsync -avz --delete --progress --stats some_directories host@ip-address:backupFolder/

NOTE: be careful with rsync's trailing slash => /. Read the man pages.

Terry Wang
  • 9,775
0

Just append the command you want to run remotely.

ssh user@ip-address rm -r backupFolder/

This runs the single command on the remote machine. The manpage of the SSH client tells you this syntax:

SYNOPSIS
     ssh [-1246AaCfgKkMNnqsTtVvXxYy] ...more options... [user@]hostname [command]

As you mentioned in your question already, one should use public key authentication to login without being asked for a password. For other visitors, see How can I set up password-less SSH login? for that.

gertvdijk
  • 67,947
0

Either you could have a script using

ssh host@ip-address << EOF
`rm foo`
exit
EOF

Or you can pipe commands to ssh using grave accents in the terminal, bypassing the need for writing a script at all. For example:

ssh host@ip-address:folder `rm foo`

Source: http://www.unix.com/302265172-post2.html

Jez W
  • 2,090