0

I'm creating a bash script to automate the task of making a backup of the virtual hosts I have in my web development virtual machine. I know there's far better approaches for this like using a backup manager, but I just want a quick & dirty method of having a physical backup of my development files in case I end screwing up the virtual machine.

Currently, this is what I currently wrote (I'm not a bash guru, so be benevolent haha):

cd sites
for d in `find -maxdepth 1 -mindepth 1 -type d`; do
mysqldump --user=XXXXXXXX --password=XXXXXXXX `echo $d | sed -r 's/^.{2}//'` > `echo $d | sed -r 's/^.{2}//'`-`date +"%d%m%Y"`.sql
if [$d -eq "./r4.dev"]; then
zip -r -9 `echo $d | sed -r 's/^.{2}//'`-`date +"%d%m%Y"`.zip {$d,`echo $d | sed -r 's/^.{2}//'`-`date +"%d%m%Y"`.sql} -x *documentation*
else
zip -r -9 `echo $d | sed -r 's/^.{2}//'`-`date +"%d%m%Y"`.zip {$d,`echo $d | sed -r 's/^.{2}//'`-`date +"%d%m%Y"`.sql} -x *framework*
fi
rm `echo $d | sed -r 's/^.{2}//'`-`date +"%d%m%Y"`.sql
done
cd ..

The logic behind the scheme is the following: all domains get a symlink to the PHP framework I'm developing, which is located in another domain (r4.dev). What I want is for the domain where the development files are to zip everything except the documentation folder. In any of the other domains, I want to zip everything except the symlink to the framework, because if it's present, it ends backing up the whole framework folder, and I want to get a minimum-sized backup.

The backup commands work per se, but what is failing is the directory comparison. I've been trying different combinations like $d -eq "./r4.dev", "$d" = ./r4.dev, "$(basename "${d}")" -eq "./r4.dev" and some others, but none seem to work.

I'm sure the solution is rather simple, but I can't seem to see it right now. Could you give me a hint on how to correct the comparison so it works like I expect?

Thanks in advance, Julio

Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37
  • 2
    You need spaces. `[ "$d" = "./r4.dev" ]` (assuming the path returned from `find` is "./r4.dev" or course). – Etan Reisner Nov 02 '14 at 20:29
  • I opted for `if test "$(basename "$d")" = "r4.dev"; then` and it worked. Thanks :) – Julio María Meca Hansen Nov 02 '14 at 20:54
  • 1
    Have a look [ShellCheck](http://www.shellcheck.net/) for check for simple errors. Though you will likely have a few additional issues with the way you've written the script, see [how to properly loop over find](http://stackoverflow.com/a/7039579/3076724) and [common bash pitfalls](http://mywiki.wooledge.org/BashPitfalls) – Reinstate Monica Please Nov 02 '14 at 21:12

0 Answers0