Chris Watts
Chris Watts

Reputation: 6725

Bash copying specific files

How can I get tar/cp to copy only files that dont end in .jar and only in root and /plugins directories?

So, I'm making a Minecraft server backup script. One of the options I wish to have is a backup of configuration files only. Here's the scenario:

  1. There are many folders with massive amounts of data in.
  2. Configuration files mainly use the following extensions, but some may use a different one:
    • .yml
    • .json
    • .properties
    • .loc
    • .dat
    • .ini
    • .txt
  3. Configuration files mainly appear in the /plugins folder
  4. There are a few configuration files in the root directory, but none in any others except /plugins
  5. The only other files in these two directories are .jar files - to an extent. These do not need to be backed up. That's the job of the currently-working plugins flag.

The code uses a mix of tar and cp depending on which flags the user started the process with. The process is started with a command, then paths are added via a concatenated variable, such as $paths = plugins world_nether mysql/hawk where arguments can be added one at a time.

How can I selectively backup these configuration files with tar and cp? Due to the nature of the configuration process, we needn't have the same flags to add into both commands - it can be seperate arguments for either command.

Here are the two snippets of code in concern: Configure paths:

# 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

---More 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

I can provide more code/explaination where neccessary.

Upvotes: 1

Views: 411

Answers (2)

Chris Watts
Chris Watts

Reputation: 6725

Final code:

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

Upvotes: 0

Zsolt Botykai
Zsolt Botykai

Reputation: 51693

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

Might help.

Upvotes: 2

Related Questions