前言
最近开了个mc服务器,为了提高数据的安全性,使用python写了个简单的备份脚本
备份存档数据 (包含三个世界的数据)
代码如下:
# -*- coding: utf-8 -*-
import os
import zipfile
import datetime
import logging
# 设置备份目录和备份文件名
backup_dir = "/home/minecraft/backups"
server_path = "/root/mc"
# 配置日志输出
logging.basicConfig(filename=os.path.join(backup_dir, "backup.log"), level=logging.ERROR,
format='%(asctime)s - %(levelname)s - %(message)s')
def backup_minecraft_server():
try:
# 切换到服务器目录
os.chdir(server_path)
# 备份world世界
world_backup_file = "world_{}.zip".format(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
with zipfile.ZipFile(os.path.join(backup_dir, world_backup_file), "w", zipfile.ZIP_DEFLATED) as archive:
for root, dirs, files in os.walk("world"):
for file in files:
archive.write(os.path.join(root, file))
# 备份world_nether世界
nether_backup_

本文介绍了一个使用Python编写的MC服务器备份脚本,通过定时任务每天凌晨备份世界数据,包括world、world_nether和world_the_end,并自动删除旧的备份,同时提及了异地备份(如OSS)和查看备份日志的功能。
最低0.47元/天 解锁文章
423

被折叠的 条评论
为什么被折叠?



