2012-01-14 80 views
1

我怎样才能让tar/cp只拷贝不以.jar结尾的文件,而只在根目录和/ plugins目录中拷贝?Bash复制特定文件

所以,我正在制作一个Minecraft服务器备份脚本。我希望的选项之一是仅备份配置文件。这里的情景:

  1. 有与海量数据的多个文件夹
  2. 配置的主要文件使用以下扩展名的,但有些人可能使用不同的一个。
    • .yml
    • 以.json
    • 的.properties
    • 的.loc
    • .DAT
    • 的.ini
    • .TXT
  3. 配置文件主要出现在/ plugins文件夹
  4. 有在根目录下的几个配置文件,但没有任何其他除/插件
  5. 的这两个目录中只有其他文件是.jar文件 - 在某种程度上。这些不需要备份。这是当前工作的插件标志的工作。

该代码使用tarcp的混合,具体取决于用户启动过程的标志。 该过程以命令开始,然后通过连接变量添加路径,如$paths = plugins world_nether mysql/hawk,其中可以一次添加一个参数。

如何选择性备份这些配置文件tarcp?由于配置过程的本质,我们不需要在两个命令中添加相同的标志 - 它可以是任何命令的单独参数。

下面是代码两个片段在关注: 配置路径:

# 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 

---更亲的东西----

# 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 

我可以提供更多的代码/交代哪里neccessary。

回答

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/ \; 

可能会有所帮助。

+0

看起来有前途的 – CJxD 2012-01-14 14:24:06

+0

不是应该的* .jar? – CJxD 2012-01-14 14:27:01

+0

好吧,这就是我现在解决的问题: paths =“$ paths $(find。-maxdepth 1 -type f!-iname'* .jar'| sed -e's /\.\/// ')“ \t \t paths =”$ paths $(find ./plugins -type f!-iname'* .jar'| sed -e's/\。\ ///')“ – CJxD 2012-01-14 15:12:19

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