-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft-world-backup.sh
executable file
·75 lines (60 loc) · 4.3 KB
/
minecraft-world-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#
# minecraft-world-backup.sh - This script will maintain backups on this server for both the primary and secondary drive.
# Version: 0.3
#
# By: Brian Nichols
# Make sure to place this in crontab to run every 6 hours:
#00 00,06,12,18 * * * /bin/bash /home/brian/repos/minecraft/minecraft-world-backup.sh >> /home/brian/maintenance/minecraft-world-backup-logs/minecraft-world-backup-`date +\%Y-\%m-\%d_\%H-\%M-\%S\%z`.log 2>&1
# Variables:
#minecraft_world_backup_primary_dir=/home/brian/minecraft-backup-primary-drive/minecraft-world-backup/
#minecraft_world_backup_secondary_dir=/home/brian/minecraft-backup-secondary-drive/minecraft-world-backup/
# Primary drive backup cleanup
# Check to ensure we have at least 56 backups, which should equal 14 days worth of backups (14 days x 4 backups per day for every 6 hours).
# if we have greater than or equal to 56 backups within the last 14 days, then delete all minecraft-world-backup-*.tar.gz files older than 14 days.
if [ "`find /home/brian/minecraft-backup-primary-drive/minecraft-world-backup/ -type f -name "minecraft-world-backup-*.tar.gz" -mtime -14 | wc -l`" -ge "56" ];
then
echo Since we have enough backups, deleting all minecraft-world-backup-*.tar.gz files older than 14 days from the primary drive backup
find /home/brian/minecraft-backup-primary-drive/minecraft-world-backup/ -type f -name "minecraft-world-backup-*.tar.gz" -mtime +14 -delete
else
echo Not enough backups on the primary drive backup, so not deleting any backups.
fi
# Secondary drive backup cleanup
# Check to ensure we have at least 56 backups, which should equal 14 days worth of backups (14 days x 4 backups per day for every 6 hours).
# if we have greater than or equal to 56 backups within the last 14 days, then delete all minecraft-world-backup-*.tar.gz files older than 14 days.
#if [ "`find /home/brian/minecraft-backup-secondary-drive/minecraft-world-backup/ -type f -name "minecraft-world-backup-*.tar.gz" -mtime -14 | wc -l`" -ge "56" ]; then
# echo Since we have enough backups, deleting all minecraft-world-backup-*.tar.gz files older than 14 days from the secondary drive backup
# find /home/brian/minecraft-backup-secondary-drive/minecraft-world-backup/ -type f -name "minecraft-world-backup-*.tar.gz" -mtime +14 -delete
#else
# echo Not enough backups on the secondary drive backup, so not deleting any backups.
#fi
# Announce in the minecraft world that the backup is starting soon and that the server will be going down.
echo Messaging screen session that backup is starting in 5 mins
screen -S minecraft-server -X stuff 'say Backup starting in 5 mins. Server will be going DOWN''\015'
# Sleep for 4 minutes.
echo sleeping for 4 mins
sleep 240
# Announce in the minecraft world that the backup is starting soon and that the server will be going down.
echo Messaging screen session that backup is starting in 1 min
screen -S minecraft-server -X stuff 'say Backup starting in 1 min. Server will be going DOWN''\015'
# Sleep for 55 seconds.
echo sleeping for 55 secs
sleep 55
# Announce in the minecraft world that the backup is starting and that the server is going down.
echo Messaging screen session that backup is starting and server is going down
screen -S minecraft-server -X stuff 'say Backup starting. Server going down NOW''\015'
# Take the minecraft world down
echo Messaging screen session to take the minecraft world down
screen -S minecraft-server -X stuff 'stop''\015'
# Sleep for 1 min for the minecraft world to stop properly
echo sleeping for 1 min
sleep 60
# Tar up the entire minecraft directory including the settings, world, etc and timestamp the tar file and place it in the primary backup location.
echo Creating backup
tar -zcvf "/home/brian/minecraft-backup-primary-drive/minecraft-world-backup/minecraft-world-backup-$(date '+%Y-%m-%d_%H-%M-%S%z').tar.gz" /home/brian/minecraft
# Take a copy of the new backup on the primary drive and place it on the secondary drive backup location.
#echo Copying backup to secondary drive
#cp -p "`ls -dtr1 /home/brian/minecraft-backup-primary-drive/minecraft-world-backup/* | tail -1`" "/home/brian/minecraft-backup-secondary-drive/minecraft-world-backup/"
# Bring up the minecraft world
echo Messaging screen session to start the minecraft world
screen -S minecraft-server -X stuff 'LD_LIBRARY_PATH=. /home/brian/minecraft/running/bedrock_server''\015'