Created
April 10, 2012 17:16
-
-
Save e0da/2352953 to your computer and use it in GitHub Desktop.
Backup minecraft files
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
#!/usr/bin/env ruby | |
# minecraft source and backup destination directories | |
SRC = '/home/force/minecraft' | |
DST = '/home/force/mincraft_backup' | |
# maximimum total size for all backups in bytes | |
MAX = 10**9 # about 1 GB | |
def backup | |
timestamp = Time.now.to_s[0..-7].gsub(/ /, '_') | |
Dir.mkdir DST unless Dir.exists? DST | |
target = "#{DST}/minecraft_backup_#{timestamp}.tar.gz" | |
puts "Backuping up #{SRC} to #{target}..." | |
`tar cjf #{target} #{SRC}` | |
if $?.exitstatus == 0 | |
puts "Backup successful" | |
else | |
puts "Backup failed" | |
end | |
end | |
def prune_old_backups | |
puts "Pruning old backups..." | |
while total_backup_size > MAX | |
`rm #{DST}/#{oldest_backup}` | |
puts "Deleted #{DST}/#{oldest_backup}" | |
end | |
end | |
def total_backup_size | |
`du -s #{DST}`.match(/^(\d+)/)[1].to_i | |
end | |
def oldest_backup | |
`ls -t #{DST}`.split("\n").last | |
end | |
def stop_minecraft | |
puts "Killing Minecraft..." | |
pid = `ps -ef|grep minecraft_server.jar|grep -v grep|awk '{print $2}'` | |
`kill #{pid}` | |
puts "Minecraft killed. :(" | |
end | |
def start_minecraft | |
puts "Starting Minecraft..." | |
`nohup java -Xms1G -Xmx2G -jar /home/force/minecraft/minecraft_server.jar nogui &` | |
puts "Minecraft started. :)" | |
end | |
stop_minecraft | |
backup | |
start_minecraft | |
prune_old_backups |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment