user1469734
user1469734

Reputation: 801

Rsync skip folder based on wildcard

Script:

ash-4.4# cat rsync-backup.sh
    #!/bin/sh
    # Usage: rsync-backup.sh <src> <dst> <label>
    if [ "$#" -ne 3 ]; then
        echo "$0: Expected 3 arguments, received $#: $@" >&2
        exit 1
    fi
    if [ -d "$2/__prev/" ]; then
        rsync -azP --delete --link-dest="$2/__prev/" "$1" "$2/$3"
    else
        rsync -azP                                   "$1" "$2/$3"
    fi
    rm -f "$2/__prev"
    ln -s "$3" "$2/__prev"

How can I change this that it skip specific folders based on a wildcard?

This folder should be skipped always:

home/forge/*/storage/framework/cache/* 
home/forge/*/vendor
home/forge/*/node_modules

But how can this be achieved? What to change in the original rsync-backup.sh file?

This is not working:

rsync -azP "$1" "$2/$3" --exclude={'node_modules', 'cache','.cache','.npm','vendor','.git'}

Upvotes: 1

Views: 1137

Answers (1)

tukan
tukan

Reputation: 17345

The --exclude={'dir1','dir2',...} does not work under sh shell. It works only under bash.

Your options are:

  1. use bash, then the --exclude={'node_modules', 'cache','.cache','.npm','vendor','.git'} will work.

  2. use multiple --exclude switches like: --exclude= statements. For example, rsync <params> --exclude='node_modules' --exclude='cache' --exclude='.cache' ...

  3. use --exclude-from, where you have a text file with list of excluded directories. Like:

rsync <params> --exclude-from='/home/user/excluded_dir_list.txt' ...

The file excluded_dir_list.txt would contain one excluded dir for line like:

node_modules
cache
.cache
.npm
vendor
.git

Upvotes: 4

Related Questions