Created
April 29, 2015 06:55
-
-
Save jhead/cd77cf80d49dd185170a to your computer and use it in GitHub Desktop.
Prunes ForgeEssentials backups (probably badly)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BASE=$(pwd) | |
TEMPDIR="temp" | |
DELAY=2 | |
echo "Running from: $BASE" | |
echo "Starting in $DELAY seconds..." | |
sleep $DELAY | |
mkdir -p ./$TEMPDIR | |
## List of all dates | |
DATES=$(ls | egrep -o '[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}' | tr '-' ' ' | sort -n | uniq) | |
## Years | |
DATE_YEARS=$(echo "$DATES" | awk '{ print $1 }' | sort -n | uniq) | |
## Iterate through years | |
for year in $DATE_YEARS; do | |
## Dates for this year | |
DATES_YEAR=$(echo "$DATES" | egrep -o "$year [0-9]{1,2} [0-9]{1,2}") | |
## Months for this year | |
MONTHS=$(echo "$DATES_YEAR" | awk '{ print $2 }' | sort -n | uniq) | |
## Iterate through months | |
for month in $MONTHS; do | |
## Days for this month | |
DAYS=$(echo "$DATES_YEAR" | egrep -o "$year $month [0-9]{1,2}" | awk '{ print $3 }'| sort -n | uniq) | |
## Iterate through days | |
for day in $DAYS; do | |
echo -n "Pruning $year-$month-$day: " | |
## Find backups | |
echo -n "Listing... " | |
FILES=$(find . -maxdepth 1 -type f -name "*$year-$month-$day*.zip") | |
NUM_FILES=$(echo "$FILES" | wc -l) | |
if [ $NUM_FILES -gt 1 ]; then | |
## Move selected backups | |
echo -n "Saving $NUM_FILE... " | |
mv $(echo "$FILES" | head -1) ./$TEMPDIR/ | |
## Update list and prune | |
echo -n "Listing... " | |
FILES=$(find . -maxdepth 1 -type f -name "*$year-$month-$day*.zip") | |
NUM_FILES=$(echo "$FILES" | wc -l) | |
if [ $NUM_FILES -gt 0 ]; then | |
echo -n "Deleting $NUM_FILES... " | |
rm -f $FILES | |
fi | |
echo -n "Moving back... " | |
mv ./$TEMPDIR/*.zip ./ | |
fi | |
echo "Done!" | |
done | |
done | |
done | |
rm -rf ./$TEMPDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment