简体   繁体   English

如何从 xargs rsync 中排除目录?

[英]How to exclude directories from xargs rsync?

So I'm trying to set up a bash script for incremental backups using rsync with xargs to use multiple threads on Ubuntu Server 20.042 LTS.所以我试图设置一个 bash 脚本用于增量备份,使用rsyncxargs在 Ubuntu 服务器 20.042 LTS 上使用多个线程。 There's a handful of directories that I want to exclude, but I can't seem to figure out the way to do that.我想排除一些目录,但我似乎无法弄清楚如何做到这一点。 Currently I'm piping ls to xargs which then executes rsync with 8 processes:目前我正在将ls传送到xargs ,然后使用 8 个进程执行rsync

# Create the archive and begin the backup.
cd /
ls --indicator-style none \
  --ignore={"bin","boot","cdrom","data","dev","init*","lib","lib64","lost+found"} \
  --ignore={"media","mnt","proc","root","run","sbin","snap","srv","swap.img","sys"} \
  --ignore={"tmp","vmlinuz*"} | xargs --max-args 1 --max-procs 8 -I % \
  rsync --archive --compress --delete --ignore-missing-args --recursive --verbose \
  --link-dest "${backup_link}" "%" \
  --exclude={"*tmp*","*.cache"} \
  "${backup_path}"

For some reason, it's still trying to use rsync on the directories that I've ignored with the ls command.出于某种原因,它仍在尝试在我用ls命令忽略的目录上使用rsync I've tried setting rsync to exclude the directories instead:我尝试将rsync设置为排除目录:

# Create the archive and begin the backup.
cd /
ls --indicator-style none | xargs --max-args 1 --max-procs 8 -I % \
  rsync --archive --compress --delete --ignore-missing-args --recursive \
  --exclude={"/bin","/boot","/cdrom","/data/mysql","/data/backup","/dev"} \
  --exclude={"/initrd.img","/initrd.img.old","/lib","/lib64","/lost+found","/media"} \
  --exclude={"/mnt","/proc","/root","/run","/sbin","/snap","/srv","/swap.img","/sys"} \
  --exclude={"**/tmp","/vmlinuz*","*.cache"} \
  --link-dest "${backup_link}" "%" "${backup_path}"

Which still tries to backup the files that I want to exclude.它仍然尝试备份我要排除的文件。

The reason I'm trying to run multiple threads is that this script is set to execute every 15 minutes via crontab and, without the multiple processes, it's barely taking less than 15 minutes.我尝试运行多个线程的原因是该脚本设置为通过crontab每 15 分钟执行一次,并且如果没有多个进程,它只需要不到 15 分钟。 Later in the script, it then copies that backup (also using rsync ) to an SMB server, which is a huge bottleneck with all of the small files.稍后在脚本中,它会将该备份(也使用rsync )复制到 SMB 服务器,这是所有小文件的巨大瓶颈。

Is there something that I'm missing here?有什么我在这里想念的吗? Or is there a better way to accomplish this?还是有更好的方法来实现这一点? This is a production server, so I'd really like to avoid installing third-party utilities.这是一个生产服务器,所以我真的很想避免安装第三方实用程序。

Thank you in advance!先感谢您!

EDIT : in my first answer, I indicated that your syntax for the --ignore option did not work on Mint 19 (ls GNU 8.28).编辑:在我的第一个答案中,我指出您的--ignore选项语法不适用于 Mint 19(ls GNU 8.28)。 Well it does, so this part is now removed!确实如此,所以这部分现在被删除了!


I always try to use find for such tasks, and not trust ls .我总是尝试将find用于此类任务,而不是信任ls You can use this:你可以使用这个:

find / -maxdepth 1 -type d ! -name "bin" -print

Use ! -name "XXX"使用! -name "XXX" ! -name "XXX" sections many times for your exclusions. - 多次为您的排除! -name "XXX"部分。


Or my preferred method, since I find it easier to read:或者我更喜欢的方法,因为我发现它更容易阅读:

find / -maxdepth 1 -type d -print | egrep -Ev "bin|boot"

You can have as many patterns inside the egrep match, separated by a |您可以在egrep匹配中包含尽可能多的模式,用|分隔。 . . In this context, |在这种情况下, | is like or .就像or So here it says to filter out "bin" or "boot".所以这里说要过滤掉“bin”或“boot”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM