正在接听我自己的问题昨天,我编写了一个 bash 脚本,它将定义的目录中的所有目录归档到它们自己单独的归档文件中。我还为文件定义了一个变量,其中包含我想跳过的目录列表。我希望它能像--排除的参数tar
,但显然这是行不通的。我能实现这一目标的最佳方法是什么?我想这grep
就是我应该走的路。
#!/bin/bash
#START
TIME=`date +%Y-%m-%d_%Hh%Mm` # Append date and time to backup file
SRCDIR=/srv # Location of directories to backup
DESDIR=/srv/backup # Destination of backup files
EXCLUDE=exclude.txt # File which defines what to exclude from archiving
for dir in $SRCDIR/*/
do
base=$(basename "$dir")
tar -cpzf $DESDIR/$base-$TIME.tar.gz $dir
done
#END
答案1
您可以使用 grep 检查目录是否列在您的排除文件中,如下所示这个答案关于SO:
#!/bin/bash
#START
TIME=`date +%Y-%m-%d_%Hh%Mm` # Append date and time to backup file
SRCDIR=/srv # Location of directories to backup
DESDIR=/srv/backup # Destination of backup files
EXCLUDE=exclude.txt # File which defines what to exclude from archiving
for dir in $SRCDIR/*/
do
base=$(basename "$dir")
if grep -Fxq "$base" $EXCLUDE
then
echo "$base excluded"
else
tar -cpzf $DESDIR/$base-$TIME.tar.gz $dir
fi
done
#END