我在Ubuntu 12.04 LTS服务器上使用rsync来备份所有的/ ,但是,每当我运行脚本(以root用户身份)时,目录/backup被包括在内 ,当它被排除时 :
#!/bin/sh rsync --password-file=/etc/rsyncd.password --delete -aAXzv --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs,/home/*/Downloads/*,/backup/*,/var/spool/squid3,/opt/atlassian,var/www/\~*} /* fsbak@servername::fsbak
为什么会发生这种情况,我怎样才能让rsync忽略/backup ?
在脚本中将#!/bin/sh改为#!/bin/bash已经解决了这个问题。 脚本中使用的大括号扩展是bash特性,因此在sh不可用。
从rsync(1)页:
--exclude=PATTERN exclude **files** matching PATTERN
指定/backup/* ,不包括目录内的所有文件 ,而不包括目录本身。 要排除目录,请使用/backup
例:
$ tree test test ├── a │ ├── 1 │ ├── 2 │ ├── 3 │ └── 4 └── b $ rsync -av --exclude=4 $PWD/test/a/ $PWD/test/b/ sending incremental file list ./ 1/ 2/ 3/ sent 87 bytes received 27 bytes 228.00 bytes/sec total size is 0 speedup is 0.00 $ tree test test ├── a │ ├── 1 │ ├── 2 │ ├── 3 │ └── 4 └── b ├── 1 ├── 2 └── 3 9 directories, 0 files