Search code examples
bashrsync

why rsync not working in bash?


I've got real simple rsync bash file looking like this :

#!bin/bash
rsync -avh --exclude={"/media/*","/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found/*"}  / /my/backup/path

but the problem is when i do :

sh mybash.sh

It forgot that i have some excluding to do and it backup every thing. the funniest of all is when i do :

rsync -avh --exclude={"/media/*","/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/lost+found/*"}  / /my/backup/path

In command line it consider my exclusion.

I need to put the bash in crontab but for this reason i can't.

Any help would be appreciated.

My OS is Debian wheezy.


Solution

  • You are executing the script with something other than bash, probably dash, which doesn't recognize brace expansion. Be explicit:

    bash mybash.sh
    

    However, brace expansion in bash is primarily meant as a shortcut for interactive use. If you are writing a script, just be explicit (your text editor should make it simple):

    #!/bin/sh
    rsync -avh \
       --exclude "/media/*" \
       --exclude "/dev/*" \
       --exclude "/proc/*" \
       --exclude "/sys/*" \
       --exclude "/tmp/*" \
       --exclude "/run/*" \
       --exclude "/mnt/*" \
       --exclude "/lost+found/*" \
       / /my/backup/path
    

    Now you don't need to worry about which shell executes the script, as long as it is POSIX compliant.

    If you really want to use bash features, I recommend using an array instead of brace expansion. It's more readable and easier to edit in the future.

    #!/bin/bash
    exclusions=(
       --exclude "/media/*"
       --exclude "/dev/*"
       --exclude "/proc/*"
       --exclude "/sys/*"
       --exclude "/tmp/*"
       --exclude "/run/*"
       --exclude "/mnt/*"
       --exclude "/lost+found/*"
    )
    rsync -avh "${exclusions[@]}" / /my/backup/path