Hey everyone! 👋 I'm diving into Bash scripting and have put together a basic backup script that creates a `.tar.gz` file of a specified directory, complete with the current date in the filename. Here's what I've come up with so far:
```bash
#!/bin/bash
echo "Welcome to the backup program"
BACKUP_FILE="backup_$(date +'%Y-%m-%d_%H-%M-%S').tar.gz"
TARGET_DIR="/mnt/f/Programming/Linux/"
if [ -d "$TARGET_DIR" ]; then
echo "Backing up..."
tar -cvpzf "$BACKUP_FILE" "$TARGET_DIR"
echo "Backup Done ✅"
else
echo "❌ Cannot create backup"
echo "Directory $TARGET_DIR does not exist"
exit 1
fi
```
While it works fine, I'm looking for suggestions from more advanced users on how to make it more robust or efficient. Tips on error handling, logging, user input, or best practices for backup organization would be greatly appreciated! 🙏
1 Answer
One thing to consider is to use lower case for your variable names instead of all caps for better readability. This helps differentiate private variables from those that might be exported. Also, if POSIX compatibility isn’t a concern, try using `[[` instead of `[` for your condition checks. It handles spaces better and is more powerful.
Thanks for sharing your thoughts!