Skip to content

Instantly share code, notes, and snippets.

@somat
Created July 12, 2024 05:05
Show Gist options
  • Save somat/3d036672a20e4e15387593ee9a4d2caf to your computer and use it in GitHub Desktop.
Save somat/3d036672a20e4e15387593ee9a4d2caf to your computer and use it in GitHub Desktop.
#!/bin/bash
# Array of target directories to backup
TARGETS=("/path/to/first_folder" "/path/to/second_folder" "/path/to/third_folder")
# Destination directory to store the backup
DESTINATION="/path/to/backup_folder"
# Retention period in days
RETENTION_DAYS=7
# Get the current date
CURRENT_DATE=$(date +%Y-%m-%d)
# Loop through the target directories
for TARGET in "${TARGETS[@]}"; do
# Get the base name of the target directory
TARGET_NAME=$(basename "$TARGET")
# Create the tar and compress the target directory
tar -czf "$DESTINATION/${TARGET_NAME}_backup_${CURRENT_DATE}.tar.gz" -C "$(dirname "$TARGET")" "$TARGET_NAME"
done
# Delete files older than the retention period
find "$DESTINATION" -type f -name "*.tar.gz" -mtime +$RETENTION_DAYS -exec rm -f {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment