What’s the Best Way to Parse a Config for a Backup Script?

0
10
Asked By SillyGoose42 On

I'm working on a backup script that manages multiple external HDDs, but I'm trying to streamline how I specify the paths associated with each drive. Instead of manually setting all the paths, I want to simply specify the drive names, which should link to their correct paths. For example, I might have a configuration that looks like: `driveA=(/pathA /pathB /pathD)`.

Currently, I'm using variable names as drive identifiers and namerefs (`declare -n`), but that has a couple of drawbacks. Notably, Bash variable names can't include dashes (`-`), which would be useful for drive naming, and I'd prefer to separate the variable definitions into a configuration file instead of keeping everything in the script.

I'm looking for recommendations on a standard way to parse a configuration in a way that will be reliable and easy to manage all these paths associated with drives. The format doesn't have to stick to what I've mentioned, but I prefer to have one config file to keep everything organized.

5 Answers

Answered By ChewyBeans88 On

Using a delimited format can simplify things! Each drive can have a unique ID, so you might consider a setup like this:

```
UUID:source path:destination path
```

You can get the UUIDs from your drives using `blkid`. Your backup script can read from this config file, allowing you to easily loop through the drives and their paths. Here's a quick example:

```bash
while read -r uuid; do
while IFS=":" read -r _ source dest; do
# Your backup steps here
done < <(grep "^${uuid}" /path/to/backup.conf)
done < <(sudo blkid -s UUID -o value)
```

This keeps it straightforward and uses simple string parsing!

Answered By ConfigMaster123 On

Using a format like TOML works great for this kind of configuration too! It can easily define your drives and paths in a clean manner that’s easy to read and maintain. You might find it less cumbersome compared to traditional bash-only solutions.

Answered By BasicUser44 On

Keep it simple! Just remember to separate your drive identifiers from the paths with a delimiter to avoid conflicts. Something like "|" works well since it's seldom found in path names!

Answered By BashNinja76 On

Honestly, if the configurations get too complex, you might want to switch to Python or another programming language that handles nested structures better than Bash. If you stick with Bash, just make sure your drive names don’t conflict with variable names and consider organizing your data effectively with delimiters. You can load paths into arrays which simplifies the way you manage multiple values.

Answered By TechGuru500 On

For something more robust, consider using a service registry like Consul or Etcd. They can store variables in a structured way and make retrieval seamless. With a service like this, you can fetch configurations using a simple command instead of worrying about formatting your own files. Here's a quick sample for fetching keys:

```bash
for key in $(curl -s http://localhost:8500/v1/kv/myapp/config/?keys); do
var_name=$(basename "$key")
var_value=$(curl -s http://localhost:8500/v1/kv/"$key"?raw)
export "$var_name"="$var_value"
done
```

This will load all your relevant configuration into environment variables automatically!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.