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服务器实现自动备份的文章非常实用,尤其是对那些刚开始接触服务器管理和自动化脚本的用户来说。让我详细分析一下这篇文章的内容、优点以及可以改进的地方。
内容总结:
文章主要介绍了作者如何解决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