Interactive backup pruner bash script
user: can you write me a script to prune on in otherwords clean up the backup directories?
I want to run the script, then it ask me a series of questions
assistant: Absolutely, Captain. Here's a script that **interactively prunes your backup directories** by asking a series of questions like:
1. Which directory to clean?
2. Keep how many most recent backups?
3. Confirm before deletion?
Then it performs the deletion accordingly.
---
### ✅ Interactive Backup Pruner Script
```bash
nano ~/prune-backups.sh && chmod +x ~/prune-backups.sh && ~/prune-backups.sh
```
Paste this in the editor:
```bash
#!/bin/bash
echo "🧹 Welcome to the Backup Pruner!"
# Step 1: Ask for directory
read -rp "📁 Enter the full path to your backup directory: " BACKUP_DIR
# Verify directory exists
if [[ ! -d "$BACKUP_DIR" ]]; then
echo "❌ Directory not found: $BACKUP_DIR"
exit 1
fi
# Step 2: How many backups to keep
read -rp "🔢 How many most recent backups do you want to keep? " KEEP_COUNT
# Validate it's a number
if ! [[ "$KEEP_COUNT" =~ ^[0-9]+$ ]]; then
echo "❌ Invalid number: $KEEP_COUNT"
exit 1
fi
# Step 3: Preview deletions
echo "🔍 Scanning $BACKUP_DIR ..."
# List files sorted by modified time (most recent first)
FILES_TO_DELETE=$(ls -tp "$BACKUP_DIR" | grep -v '/$' | tail -n +$((KEEP_COUNT + 1)))
if [[ -z "$FILES_TO_DELETE" ]]; then
echo "✅ No old backups to delete. You're good!"
exit 0
fi
echo "🗑️ The following backups will be deleted:"
echo "$FILES_TO_DELETE"
echo
read -rp "⚠️ Are you sure you want to delete these files? (y/N): " CONFIRM
if [[ "$CONFIRM" != "y" && "$CONFIRM" != "Y" ]]; then
echo "❎ Aborted. No files were deleted."
exit 0
fi
# Step 4: Delete files
cd "$BACKUP_DIR" || exit 1
echo "$FILES_TO_DELETE" | xargs -d '\n' rm -v
echo "✅ Pruning complete!"
```
---
Let me know if you want to:
- Add date filtering (e.g. delete older than 30 days)
- Exclude certain patterns (e.g. `.sql` dumps)
- Add logging or a dry-run mode
I’ll adapt the script instantly.
user: uhm, i said i want to keep one backup and I see 4 others still
and i think you forgot about the mysql folder
adam@MCWAI:~$ ~/prune-backups.sh
🧹 Welcome to the Backup Pruner!
📁 Enter the full path to your backup directory: /backups
🔢 How many most recent backups do you want to keep? 1
🔍 Scanning /backups ...
🗑️ The following backups will be deleted:
backup-20250531-000001.tar.gz
backup-20250530-120001.tar.gz
backup-20250530-000001.tar.gz
backup-20250529-120001.tar.gz
⚠️ Are you sure you want to delete these files? (y/N): y
rm: cannot remove 'backup-20250531-000001.tar.gz': Permission denied
rm: cannot remove 'backup-20250530-120001.tar.gz': Permission denied
rm: cannot remove 'backup-20250530-000001.tar.gz': Permission denied
rm: cannot remove 'backup-20250529-120001.tar.gz': Permission denied
✅ Pruning complete!
adam@MCWAI:~$ cd /backups/
adam@MCWAI:/backups$ ls
backup-20250529-120001.tar.gz backup-20250531-000001.tar.gz
backup-20250530-000001.tar.gz backup-20250531-083226.tar.gz
backup-20250530-120001.tar.gz mysql
adam@MCWAI:/backups$ cd mysql
adam@MCWAI:/backups/mysql$ ls
backup-20250525-062159 backup-20250527-000001 backup-20250529-120001
backup-20250525-120001 backup-20250527-120001 backup-20250530-000001
backup-20250525-122033 backup-20250528-000001 backup-20250530-120001
backup-20250526-000001 backup-20250528-120001 backup-20250531-000001
backup-20250526-120001 backup-20250529-000001 backup-20250531-083226
adam@MCWAI:/backups/mysql$
assistant: Thanks for testing and pointing that out, Captain. Here's what happened:
### 🔍 Problems:
1. **Permission Denied**: The script didn't use `sudo` to delete the backups.
2. **Still Seeing 4 Others**: It did try to delete 4, but failed due to lack of permissions.
3. **Forgot the MySQL Subfolder**: The script didn't touch `/backups/mysql/`, but you likely want the same pruning logic applied there too.
---
### ✅ Fixed Script: Recursive, Permission-Aware, and Handles `mysql/` Folder
This updated script will:
- Prompt for main backup dir (e.g. `/backups`)
- Ask how many backups to keep
- Prune both main `.tar.gz` backups **and** the `/mysql` subfolder
- Use `sudo` automatically for deletion
- Offer confirmation before deleting each group
---
### 🧼 NEW SCRIPT
```bash
nano ~/prune-backups.sh && chmod +x ~/prune-backups.sh && ~/prune-backups.sh
```
Paste this in:
```bash
#!/bin/bash
echo "🧹 Welcome to the Backup Pruner!"
read -rp "📁 Enter the full path to your backup directory: " BACKUP_DIR
[[ ! -d "$BACKUP_DIR" ]] && echo "❌ Directory not found!" && exit 1
read -rp "🔢 How many most recent backups do you want to keep in each folder? " KEEP_COUNT
[[ ! "$KEEP_COUNT" =~ ^[0-9]+$ ]] && echo "❌ Not a number!" && exit 1
prune_folder() {
local folder="$1"
echo -e "\n📂 Checking: $folder"
local files
files=$(ls -tp "$folder" | grep -v '/$' | grep -E 'backup.*' | tail -n +$((KEEP_COUNT + 1)))
if [[ -z "$files" ]]; then
echo "✅ Nothing to prune in $folder"
return
fi
echo "🗑️ These files in $folder will be deleted:"
echo "$files"
read -rp "⚠️ Proceed to delete them? (y/N): " CONFIRM
if [[ "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "$files" | while read -r f; do
sudo rm -v "$folder/$f"
done
else
echo "⏩ Skipped $folder"
fi
}
# Prune main backup directory
prune_folder "$BACKUP_DIR"
# Prune mysql subfolder if it exists
if [[ -d "$BACKUP_DIR/mysql" ]]; then
prune_folder "$B…
Showing first 4 of 4 messages. View the full conversation at https://mcwai.org/ai-conversation/interactive-backup-pruner-bash-script