-
-
Save AgentLoneStar007/f3e7fb5729c5be75c6e01854c399cae5 to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| # Set the script to stop when an error occurs. | |
| set -eo pipefail | |
| # Get the current date as a variable | |
| printf -v DATE '%(%m-%d-%Y)T' | |
| # Get the server's directory location and make sure we're in that directory | |
| SERVER_DIR_PATH=$(dirname "$(realpath $0)") | |
| cd $SERVER_DIR_PATH | |
| # Script variables. Customize to your liking. | |
| BACKUP_FILE_NAME="Server $DATE Backup" | |
| ## $PWD will put the backups folder in the server directory. If you want | |
| ## to provide a directory outside of the server's directory, put the | |
| ## absolute path to the directory here. | |
| BACKUP_DIRECTORY="$PWD/backups/" | |
| # Announce server backup in console | |
| echo "Started server backup..." | |
| # Append the file extension to the file name and create the counter var | |
| NAME="${BACKUP_FILE_NAME}.zip" | |
| COUNTER=1 | |
| # Create the backups directory if it doesn't exist | |
| echo "Checking for backup directory..." | |
| if [ ! -d "$BACKUP_DIRECTORY" ]; then | |
| echo "Backups directory does not exist. Creating folder..." | |
| mkdir -p "$BACKUP_DIRECTORY" | |
| fi | |
| # Now, check if a file with the current name already exists inside the backups directory | |
| # If it does, a while loop will increment the counter and create a new filename until a unique one is found | |
| while [ -f "$BACKUP_DIRECTORY/$NAME" ]; do | |
| NAME="${BACKUP_FILE_NAME}-${COUNTER}.zip" | |
| COUNTER=$((COUNTER + 1)) | |
| done | |
| # Get the name of this script | |
| SCRIPT_NAME=$(basename "$0") | |
| # Compress the files, ignoring this script and the backup directory | |
| zip -r -q "$NAME" * -x "$(basename "$BACKUP_DIRECTORY/")/*" "$SCRIPT_NAME" | |
| # And move the archive to the backups directory | |
| mv "$NAME" "$BACKUP_DIRECTORY" | |
| echo "Backup completed! File located at \"$BACKUP_DIRECTORY/$NAME\"." |
Thanks for reminding me this existed.
Along with that issue, this script will also include any existing backups (for a possibly huge file size), it will overwrite any previous backups made the same day without confirmation, and it will use the directory layout from the root file system as the directory layout in the archive. (Example: when you create a backup, it will use the directory layout "/home/user/Desktop/Minecraft Server/" if your server is at that directory.) I'll rewrite this and replace it with a better version when I can.
Updated. The script will now archive the files in a directory layout relative to the server's directory and not the root directory, it will create a new backup every time the script is used, and it will ignore the backups directory in the archive.
Ha, that was all what I was also modifying it to fix, got mostly the same results, but the counter is a good idea that I hadn't thought of.
Thanks for the update! The error check at the top is nice too :D
was modifying this a little and troubleshooting, I believe
export name="Server $date Backup.zip"beexport NAME='Server $date Backup.zip"instead since the rest of the variable references it as$NAME