我怎样才能让tar / cp只复制那些不以.jar结尾的文件,只复制root和/ plugins目录?
所以,我正在制作一个Minecraft服务器备份脚本。我希望拥有的其中一个选项是仅备份配置文件。这是场景:
代码使用tar
和cp
的混合,具体取决于用户启动进程的标志。
该过程以命令启动,然后通过连接变量添加路径,例如$paths = plugins world_nether mysql/hawk
,其中可以一次添加一个参数。
如何使用tar
和cp
有选择地备份这些配置文件?由于配置过程的性质,我们不需要在两个命令中添加相同的标志 - 它可以是任一命令的单独参数。
以下是关注的两段代码: 配置路径:
# My first, unsuccessful attempt.
if $BKP_CFG; then
# Tell user they are backing up config
echo " +CONFIG $confType - NOT CURRENTLY WORKING"
# Main directory, and everything in plugin directory only
# Jars are not allowed to be backed up
#paths="$paths --no-recursion * --recursion plugins$suffix --exclude *.jar"
fi
---更多Pro Stuff ----
# Set commands
if $ARCHIVE; then
command="tar -cpv"
if $COMPRESSION; then
command=$command"z"
fi
# Paths starts with a space </protip>
command=$command"C $SERVER_PATH -f $BACKUP_PATH/$bkpName$paths"
prep=""
else
prep="mkdir $BACKUP_PATH/$bkpName"
# Make each path an absolute path. Currently, they are all relative
for path in $paths; do
path=$SERVER_PATH/$path
done
command="cp -av$paths $BACKUP_PATH/$bkpName"
fi
我可以在必要时提供更多代码/解释。
答案 0 :(得分:2)
find /actual/path ! -iname '*jar' -maxdepth 1 -exec cp \{\} /where/to/copy/ \;
find /actual/path/plugins ! -iname '*jar' -maxdepth 1 -exec cp \{\} /where/to/copy/ \;
可能有帮助。
答案 1 :(得分:0)
最终代码:
if $BKP_CFG; then
# Tell user what's being backed up
echo " +CONFIG $confType"
# Main directory, and everything in plugin directory only
# Jars are not allowed to be backed up
# Find matches within the directory cd'd to earlier, strip leading ./
paths="$paths $(find . -maxdepth 1 -type f ! -iname '*.jar' | sed -e 's/\.\///')"
paths="$paths $(find ./plugins -type f ! -iname '*.jar' | sed -e 's/\.\///')"
fi