0

Below is a script I've wrote to backup my routers and adsl modem, Any ideas to compact this script in less lines?

Each router have a different kind of structure as you can see.

#!/bin/sh
http_txt="10.0.0.59"
cgi="10.0.0.60"
http="10.0.0.66"
date="$(date +%d-%m-%y)"
dest="/home/yamash/"
archive_file="$dest$date.tgz"
local="$dest$date"
login="admin"
pass="PASSWORD"

mkdir $local
cd "$local"
echo "Iniciando Backup"

for http_txt_for in $http_txt; do
    echo "$http_txt_for"
    wget --auth-no-challenge --user=$login --password=$pass $http_txt_for/config.dat -O $http_txt_for.dat
done

for cgi_for in $cgi; do
    echo "$cgi_for"
    wget --user=$login --password=$pass $cgi_for/cgi-bin/export_settings.cgi -O $cgi_for.dat
done

for http_for in $http; do
    echo "$http_for"
    wget --user=$login --password=$pass $http_for/rom-0 -O $http_for
done

cd ..
tar czf $archive_file $local
#rm -R $archive_file $local
echo "Backup Finalizado"
2
  • 1
    This should probably be on CR.SE
    – l0b0
    May 4, 2014 at 21:32
  • I'm sorry for posting in wrong place. May 5, 2014 at 13:27

1 Answer 1

1

Some possibilities:

  • You don't need to loop if the variable only has a single value.
  • Use verbose mode instead of echoing debug information. echos like this are like comments: They aren't code and they may get obsolete. Printing what's actually going on is much more useful for debugging.
  • You can use wget --output-document=/path/to/file to avoid cding before wgetting.
  • You can use tar -C .. to go to the parent directory before doing the tar operation without cd.
  • Why create a directory and a file name with the date? You only need a date indicator in the tarball name. The canonical solution for this sort of thing is to

    tmp="$(mktemp -d)"
    trap -- 'rm -frv -- "$tmp"' EXIT
    cd -- "$tmp"
    

    and store the temporary files there. This also removes the need for the previous two tips.

  • If you only need the date once, you can inline $date.
1
  • Thanks for your suggestions, I will edit my script and after I will post it in main topic, The Ip was a example, I will use more than one IP for variable. May 5, 2014 at 14:46

You must log in to answer this question.

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