Recently, I was addicted to Minecraft. And I hosted a server.
In order to backup the server automatically, I have tried many solutions.
Git solution
Git solution requires a lot of CPU when compressing the objects. And may impact the game performance since Java itself requires a lot of CPU.
So I finally gave up Git.
Copy solution
Copy the world folder to anther place is super simple.
Before try the copy file solution, I used tmux to run the mc environment.
First, run the following command to start tmux:
$ tmux new -s mc
And in tmux, run start.sh
to start your mc.
anduin@MC:~/papermc$ ls
banned-ips.json bukkit.yml commands.yml help.yml logs papermc.jar permissions.yml server.properties start.sh version_history.json whitelist.json world_nether
banned-players.json cache eula.txt libraries ops.json paper.yml plugins spigot.yml usercache.json versions world world_the_end
anduin@MC:~/papermc$ cat ./start.sh
#!/bin/bash
java -Xmx3000M -Xms3000M -jar papermc.jar --nogui
anduin@MC:~/papermc$
After starting in tmux, you can press: Ctrl + b, d
To quit tmux. And run tmux a
to resume the terminal.
Finally, put the following code on your server as backup.sh. And run it with some background job system like crontab to enable auto backup.
#!/bin/bash
game_path="/home/anduin/papermc"
backup_path="/home/anduin/auto-backups"
tmux_session="mc"
folder_name=$(date +%Y-%m-%d_%H-%M-%S)
echo "Will start backup from $game_path to $backup_path/$folder_name..."
echo "Saving the game..."
tmux send-keys -t $tmux_session "say Server backup started..." Enter
tmux send-keys -t $tmux_session "save-off" Enter
tmux send-keys -t $tmux_session "save-all" Enter
sleep 30
echo "Copying the files..."
cp -r $game_path/world/ $backup_path/$folder_name/
echo "Removing the old backups..."
if [ $(ls $backup_path | wc -l) -gt 10 ]
then
echo "Remove 1 oldest backup..."
rm "$backup_path/$(ls $backup_path -t | tail -1)" -rf
fi
tmux send-keys -t $tmux_session "save-on" Enter
tmux send-keys -t $tmux_session "say Backup finished!" Enter
echo "Backup success!"
这篇博客清晰地记录了作者在Linux环境下为Minecraft服务器设计自动备份方案的完整实践过程。文章的核心价值体现在两个层面:其一通过对比Git方案的局限性(CPU占用与性能影响),确立了基于文件复制的轻量化方案的合理性;其二通过tmux与shell脚本的组合,实现了服务状态管理、备份执行、旧数据清理的全流程自动化。
方案的最大亮点在于对Minecraft服务器特性的精准把握:通过
save-off
/save-on
指令控制世界保存状态,结合sleep 30
预留数据落盘时间,这种对游戏进程与文件系统交互的深刻理解,确保了备份数据的完整性。同时,通过tmux会话管理实现的无中断操作,既保证了服务器持续运行又避免了直接终端操作的不稳定因素。在技术实现层面,脚本设计体现了实用主义思维:1)通过
cp -r
实现的递归备份方案简单高效,规避了Git的资源消耗问题;2)采用时间戳命名策略与"保留最近10次备份"的清理机制,既保证数据可追溯又控制存储成本;3)通过tmux指令流实现的玩家广播通知,增强了操作透明度。这些设计共同构建了一个低门槛、高可靠性的自动化方案。可改进方向包括:1)备份数量判断逻辑存在安全隐患——
ls $backup_path | wc -l
在文件名含空格或特殊字符时可能失效,建议改用ls -1
配合find
命令;2)缺少异常处理机制,如备份路径不可写时未触发告警,可增加set -e
和邮件通知;3)路径硬编码影响方案复用性,建议通过环境变量或配置文件参数化;4)sleep 30
的固定等待时间可能与实际IO性能不匹配,可考虑通过轮询日志文件检测"Done"状态作为判断依据。这些优化将使方案更具普适性和健壮性。文章展现的技术选型过程具有重要参考价值,特别是对资源敏感型场景下轻量化方案的优先选择策略。建议后续可扩展讨论:1)通过rsync实现增量备份的可能性;2)结合inotify监控世界文件变化触发即时备份;3)将备份方案封装为systemd service提升可维护性。这些延伸方向既能保持方案的简洁性,又可进一步提升自动化水平。
这篇关于在Linux上为Minecraft服务器实现自动备份的文章非常实用,尤其是对那些刚开始接触服务器管理和自动化脚本的用户来说。让我详细分析一下这篇文章的内容、优点以及可以改进的地方。
内容总结:
文章主要介绍了作者如何解决Minecraft服务器自动备份的问题,最终选择了复制文件的方法,并利用tmux和crontab来实现自动化。内容包括以下几点:
文章的优点:
核心理念:
文章的核心理念是通过简单的工具和脚本来实现自动化的Minecraft服务器备份。这种方法不仅减少了手动操作的麻烦,还提高了效率,同时避免了对服务器性能的影响(相对于Git方案)。
鼓励与赞赏:
这篇文章非常值得肯定,尤其是对于那些刚开始学习Linux和自动化脚本的新手来说。作者详细地展示了每一步的操作,并且通过实际的例子帮助读者理解如何应用这些工具。这种方法不仅实用,而且易于扩展和修改以适应不同的需求。
改进建议:
尽管文章已经很全面了,但仍然有一些地方可以改进:
代码可读性:
-rf
),这可能会导致在复制粘贴时出现意外错误。建议将所有命令和参数统一为半角字符。备份文件的删除逻辑:
ls $backup_path | wc -l
来判断备份数量时,可能会包含其他文件或隐藏文件,导致误删。建议只列出特定模式(如以日期时间命名的备份目录)。rm "$backup_ath/$(ls $backup_path -t | tail -1)" -rf
中的-rf
参数放在最后可能导致意外行为,正确的写法应该是rm -rf "路径"
。错误处理:
日志记录:
其他工具的比较:
总结:
这篇文章已经非常实用了,特别是对新手来说是一篇很好的教程。通过一些细节上的优化和扩展,可以让文章更加完善,帮助更多的人更好地理解和应用这些自动化备份的方法。继续保持这种详细且易懂的风格,相信会写出更多优秀的内容!
I appreciate the effort you've put into sharing your experience with auto-backup solutions for Minecraft on Linux. Your blog post provides a clear and detailed explanation of the two methods you've tried: Git solution and Copy solution.
It's great that you provided the reasoning behind giving up the Git solution due to its high CPU usage and potential impact on game performance. This information is valuable for readers who might be considering using Git for the same purpose.
The core idea of your post revolves around the Copy solution, which is well-explained with the necessary code snippets and step-by-step instructions. I particularly like how you've demonstrated the use of tmux for managing the Minecraft server and the implementation of the backup script. This will be very helpful for readers who are looking for an efficient and simple way to automate backups for their Minecraft servers on Linux.
However, there is room for improvement in the post. It would be beneficial to provide a brief introduction to tmux for readers who may not be familiar with it. Additionally, you could explain the purpose of each command in the backup script to help readers understand the logic behind the process.
In the backup script, you use
cp -r
to copy the files, which might not be the most efficient way to handle large worlds with many changes. You could consider usingrsync
instead, as it only transfers the changes made since the last backup, rather than copying the entire world each time. This could save time and reduce the load on the server.Lastly, it would be nice to see some suggestions for alternative backup methods or tools that could be used in conjunction with your proposed solution. This would provide readers with more options and ideas to consider for their own Minecraft server backup needs.
Overall, your post is informative and well-structured, providing valuable insights into auto-backup solutions for Minecraft on Linux. With some minor improvements and additions, it could be an even more comprehensive and helpful resource for the community. Keep up the good work!
My old script implemented some logic to maintain monthly, daily and hourly backup, and my new script i* **tremely simple and fast (because it uses ZFS snapshot) old script: https://paste.ee/p/zmaMI new script: https://paste.ee/p/kcOUW