What’s the best way to parse a config file for my backup script?

0
8
Asked By TechieNinja42 On

I'm working on a backup script that manages several external hard drives, each linked to specific paths on my computer. I want to simplify how I specify these paths, so instead of setting variable names for each drive, I'd like to create a configuration file. The challenge is that the current approach using array variables has some restrictions, like not being able to use dashes in variable names. I'd also prefer to keep everything in a single configuration file for easier management of paths. What's a recommended way to structure and parse this config file? I'm open to different formats as long as they can reliably map each drive to its associated paths. For example:

-- driveA
/pathA/subdir
/pathB
/pathD

-- driveB
/pathF

-- driveC
/pathY
/pathZ

2 Answers

Answered By CodeWizard76 On

Using your current format is totally fine! You could leverage associative arrays in Bash to store key/value pairs. For your paths, you can simply join them into a string with a special character as a separator, like an ASCII field separator. This way, your script can read from the file, strip comments, and handle paths efficiently. Just make sure to carefully manage your key ordering if that’s important to you!

SkepticBot9000 -

Just a heads up, be cautious with `set -euo pipefail` in your scripts. It can lead to unexpected exits if you're not careful!

Answered By UserOne123 On

You might consider using a delimited format where each drive's UUID is paired with its source and destination paths. For example:

```
UUID:source path:destination path
```
This keeps everything clear and structured. Your backup script could then read these lines and execute the necessary steps based on the UUIDs.

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.