0

I am writing a backup script which will download all backups begins with hostname f.e MikroTik which is contained in variable device. When I put command in terminal it will download all files I need, but when I put it tot script, then it show me message file Mikrotik not found.

Here is the code sample:

#!/bin/bash
user="admin"
address="IP"
#command will get devcie hostname in complex form 
ssh $user@$address '/system identity print interval=' > devices

#will get device hostname
tmpDevices=$(cat devices)

device=$(echo $tmpDevices | awk ' {print $2} ')
echo "Device hostname is $device"

echo "Connecting to device via SFTP and downloading files"
#sftp "${user}@${address}:/${device}*"
sftp $user@$address:$device* 

Here is the output:

Device hostname is MikroTik
MikroTik
Connecting to device via SFTP and downloading files
Connected to 10.120.0.253.
File "/MikroTik" not found.

Can you please advice me.

Adrian.

8
  • What is /system? Does there exist a file/directory on 10.120.0.253 with a directory off root that starts with the hostname? The error File "/MikroTik" not found. seems self-explanatory. Dec 7, 2017 at 15:13
  • It is on mikrotik so i tried with / or without and no success Dec 7, 2017 at 15:18
  • That doesn't answer any of my questions. Do there exist files in the root directory on that host that begin with MikoTik? Dec 7, 2017 at 15:20
  • yeah there is just root filesystem / Dec 7, 2017 at 15:33
  • Please add the output of the following command to your question above: ssh ${user}@${address} "ls /MikroTik*". Fill in user/address appropriately. Dec 7, 2017 at 15:50

2 Answers 2

1

You might consider using scp instead of sftp. Assuming that you want to copy files from the root directory on the remote host to the current directory on the local host:

scp "${user}@${address}:/${device}*" . 

For example, I've created the following files on a remote host:

$ ssh user@remoteHost "ls /MikroTik*"
/MikroTik_dec07backup.backup
/MikroTik_dec07export.rsc
/MikroTik_dec07log.txt

From my local host, I can do:

$ mkdir /tmp/example
$ cd /tmp/example
$ scp "user@remoteHost:/MikroTik*" .
MikroTik_dec07backup.backup                   100%    0     0.0KB/s   00:00
MikroTik_dec07export.rsc                      100%    0     0.0KB/s   00:00
MikroTik_dec07log.txt                         100%    0     0.0KB/s   00:00
$ ls
MikroTik_dec07backup.backup  MikroTik_dec07export.rsc  MikroTik_dec07log.txt
$
6
  • no success with that also, no file found but the files are there Dec 7, 2017 at 15:37
  • Did you include the quotes as specified? Dec 7, 2017 at 15:53
  • I was battling with this 4 hours yesterday nothing worked Dec 7, 2017 at 15:54
  • Again, you didn't answer my question. When someone asks a question, that generally means that they need additional information in order to move forward. Dec 7, 2017 at 15:56
  • yeah, i tried it as said, no file found also from terminal with proper parameters it was impossible to download them, no luck there mate Dec 7, 2017 at 15:58
-1
adrian@adrian-ThinkPad-X230:~/Desktop/zaloha$ sftp admin@IP:/MikroTik*
Connected to IP.
Fetching /MikroTik_dec07backup.backup to MikroTik_dec07backup.backup
/MikroTik_dec07backup.backup                                                                                                                                                  100%   10KB  10.5KB/s   00:00    
Fetching /MikroTik_dec07export.rsc to MikroTik_dec07export.rsc
/MikroTik_dec07export.rsc                                                                                                                                                     100%  329     0.3KB/s   00:00    
Fetching /MikroTik_dec07log.txt to MikroTik_dec07log.txt
/MikroTik_dec07log.txt                                                                                                                                                        100%   75KB  75.1KB/s   00:00 

When I use it ion script as :

#!/bin/bash
user="admin"
address="IP"
#command will get devcie hostname in complex form 
ssh $user@$address '/system identity print interval=' > devices

#will get device hostname
tmpDevices=$(cat devices)

device=$(echo $tmpDevices | awk ' {print $2} ')
echo "Device hostname is $device"

echo "Connecting to device via SFTP and downloading files"
#sftp "${user}@${address}:/${device}*"
sftp $user@$address:$device* 

Output is:

Connecting to device via SFTP and downloading files
Connected to address.
File "/MikroTik" not found.
3
  • Not sure why you copied your question as an answer? Dec 7, 2017 at 16:08
  • I think this should be an edit to your question...
    – Jeff Schaller
    Dec 7, 2017 at 16:08
  • wanna help me or not? I am trying to get answers and your response is what I found on google guys and nothing helped Dec 7, 2017 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .