Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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"
share|improve this question
1  
This should probably be on CR.SE –  l0b0 May 4 at 21:32
    
I'm sorry for posting in wrong place. –  André Faria May 5 at 13:27

1 Answer 1

up vote 1 down vote accepted

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.
share|improve this answer
    
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. –  André Faria May 5 at 14:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.