Skip to content

Instantly share code, notes, and snippets.

@cool-mist
Last active December 26, 2020 14:57
Show Gist options
  • Save cool-mist/ea3b4ecef6324cc0cb1591de70f526a7 to your computer and use it in GitHub Desktop.
Save cool-mist/ea3b4ecef6324cc0cb1591de70f526a7 to your computer and use it in GitHub Desktop.
Backup minecraft world and clear stale backups
#!/bin/bash
DATE=`date`
BASE_DIR="{set-a-base-dir}"
FOLDER_TO_COMPRESS="$BASE_DIR/minecraft"
BACKUPS_FOLDER="$BASE_DIR/backups"
FILE_NAME_FORMAT=`date +"%Y-%h-%d_%H-%M"`
TAR_FILE="$BACKUPS_FOLDER/$FILE_NAME_FORMAT.tar.gz"
LOG_DIR="$BACKUPS_FOLDER/logs"
LOG_FILE="$LOG_DIR/$FILE_NAME_FORMAT.log"
mkdir -p $LOG_DIR
echo "Backing up world $FOLDER_TO_COMPRESS at $DATE to $TAR_FILE" >> $LOG_FILE
tar -zcvf $TAR_FILE $FOLDER_TO_COMPRESS >> $LOG_FILE
FILE_SIZE=`du $TAR_FILE -h | cut -f 1`
echo "Finished backing up $TAR_FILE of size $FILE_SIZE" >> $LOG_FILE
echo "Clearing backups" >> $LOG_FILE
MAX_FILES=5
cd $BACKUPS_FOLDER
FILES=`ls -1t *gz | wc -l`
if [ $FILES -gt 2 ]
then
echo "There are $FILES files, Max allowed is $MAX_FILES" >> $LOG_FILE
TO_DELETE=`expr $FILES - $MAX_FILES`
echo "Deleting $TO_DELETE oldest backups" >> $LOG_FILE
ls -1t *gz | tail -n $TO_DELETE | xargs --verbose -d '\n' rm -f >> $LOG_FILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment