P
P
partisan422020-09-24 12:08:17
linux
partisan42, 2020-09-24 12:08:17

What could be wrong with the script?

Good day.
There is a task to automate Mikrotik backups. I found a script on the net that should help me with this task.

#/bin/bash
routers=( 192.168.0.1 )
backupdir="/root/mikrotik/"
privatekey="/root/.ssh/id_dsa"
login="admin"
DATE="`date '+%Y-%m-%d'`"
for r in ${routers[@]}; do
cmd_backup="/system backup save name=${r}.backup"
ssh ${login}@$r -i $privatekey "${cmd_backup}" > /dev/null
sleep 2
cmd_backup="/export file=${r}"
ssh ${login}@$r -i $privatekey "${cmd_backup}" > /dev/null
sleep 5
scp -i $privatekey ${login}@${r}:${r}.backup ${backupdir}$r-$DATE.backup
scp -i $privatekey ${login}@${r}:${r}.rsc ${backupdir}$r-$DATE.rsc
ssh ${login}@$r -i $privatekey "/file remove \"${r}.backup\""
ssh ${login}@$r -i $privatekey "/file remove \"${r}.rsc\""
done
find $backupdir* -mtime +3 -exec rm {} \;


But when I try to execute it, I get an error.
[[email protected] ~]# ./backup_mikrotik
'/backup_mikrotik: line 7: syntax error near unexpected token `do
'/backup_mikrotik: line 7: `for r in ${routers[@]}; do

Please tell me what could be the problem? Or at least indicate in which direction to look?
Thank you very much in advance.
Alas, the task must be solved urgently, because there is simply no time physically to dive into the study of bash :(

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
R
res2001, 2020-09-24
@partisan42

You have an error in the line After for there is a unit, but there should be r. Compare with the original script you provided. Well, according to the text of the script, a loop variable is used inside forfor 1 in ${routers[@]}; do
${r}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question