password in env
This commit is contained in:
+43
-15
@@ -10,21 +10,36 @@ from db.mongo_manager import MONGO_DB_URL
|
||||
MONGO_HOST = "mongo.lpaconsulting.fr"
|
||||
MONGO_PORT = "27017"
|
||||
MONGO_DB_NAME = "appointment" # 你要备份/恢复的数据库名
|
||||
MONGO_USER = "appointment" # 如果没有密码,保持为空字符串 ""
|
||||
MONGO_PASS = "Rdv@2022" # 如果没有密码,保持为空字符串 ""
|
||||
|
||||
# Get MongoDB credentials from environment variables
|
||||
MONGO_USER = os.getenv(
|
||||
"MONGO_USER", "appointment"
|
||||
) # Default to 'appointment' if not set
|
||||
MONGO_PASS = os.getenv("MONGO_PASS", "Rdv@2022") # Default to 'Rdv@2022' if not set
|
||||
|
||||
# 备份存放的根目录
|
||||
BACKUP_DIR_ROOT = "./mongo_backups"
|
||||
|
||||
# ===========================================
|
||||
|
||||
|
||||
def get_auth_args():
|
||||
"""构建认证参数列表"""
|
||||
args = []
|
||||
if MONGO_USER and MONGO_PASS:
|
||||
args.extend(["--username", MONGO_USER, "--password", MONGO_PASS, "--authenticationDatabase", "appointment"])
|
||||
args.extend(
|
||||
[
|
||||
"--username",
|
||||
MONGO_USER,
|
||||
"--password",
|
||||
MONGO_PASS,
|
||||
"--authenticationDatabase",
|
||||
"appointment",
|
||||
]
|
||||
)
|
||||
return args
|
||||
|
||||
|
||||
def backup_mongo():
|
||||
"""执行备份操作"""
|
||||
# 1. 创建带有时间戳的备份文件夹
|
||||
@@ -40,10 +55,14 @@ def backup_mongo():
|
||||
# 命令格式: mongodump --host <host> --port <port> --db <db> --out <path> [auth]
|
||||
cmd = [
|
||||
"mongodump",
|
||||
"--host", MONGO_HOST,
|
||||
"--port", MONGO_PORT,
|
||||
"--db", MONGO_DB_NAME,
|
||||
"--out", backup_path
|
||||
"--host",
|
||||
MONGO_HOST,
|
||||
"--port",
|
||||
MONGO_PORT,
|
||||
"--db",
|
||||
MONGO_DB_NAME,
|
||||
"--out",
|
||||
backup_path,
|
||||
]
|
||||
|
||||
# 添加认证参数
|
||||
@@ -54,13 +73,14 @@ def backup_mongo():
|
||||
result = subprocess.run(cmd, check=True, text=True, capture_output=True)
|
||||
print(f"[+] 备份成功!")
|
||||
print(f" 存储路径: {backup_path}")
|
||||
print(f" 日志: {result.stderr}") # mongodump 通常把进度输出到 stderr
|
||||
print(f" 日志: {result.stderr}") # mongodump 通常把进度输出到 stderr
|
||||
return backup_path
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"[-] 备份失败: {e}")
|
||||
print(f" 错误信息: {e.stderr}")
|
||||
return None
|
||||
|
||||
|
||||
def restore_mongo(backup_source_path):
|
||||
"""
|
||||
执行恢复操作
|
||||
@@ -72,7 +92,9 @@ def restore_mongo(backup_source_path):
|
||||
target_dir = os.path.join(backup_source_path, MONGO_DB_NAME)
|
||||
|
||||
if not os.path.exists(target_dir):
|
||||
print(f"[-] 错误: 在路径 {backup_source_path} 下找不到数据库 {MONGO_DB_NAME} 的备份文件。")
|
||||
print(
|
||||
f"[-] 错误: 在路径 {backup_source_path} 下找不到数据库 {MONGO_DB_NAME} 的备份文件。"
|
||||
)
|
||||
return
|
||||
|
||||
print(f"[*] 开始恢复数据库: {MONGO_DB_NAME} 从 {target_dir} ...")
|
||||
@@ -81,11 +103,14 @@ def restore_mongo(backup_source_path):
|
||||
# 命令格式: mongorestore --host <host> --port <port> --db <db> <path_to_bson_files> [auth]
|
||||
cmd = [
|
||||
"mongorestore",
|
||||
"--host", MONGO_HOST,
|
||||
"--port", MONGO_PORT,
|
||||
"--db", MONGO_DB_NAME,
|
||||
"--host",
|
||||
MONGO_HOST,
|
||||
"--port",
|
||||
MONGO_PORT,
|
||||
"--db",
|
||||
MONGO_DB_NAME,
|
||||
"--drop", # 警告:这会在恢复前删除现有集合,确保数据干净。根据需要移除此项。
|
||||
target_dir
|
||||
target_dir,
|
||||
]
|
||||
|
||||
cmd.extend(get_auth_args())
|
||||
@@ -98,6 +123,7 @@ def restore_mongo(backup_source_path):
|
||||
print(f"[-] 恢复失败: {e}")
|
||||
print(f" 错误信息: {e.stderr}")
|
||||
|
||||
|
||||
# ================= 主程序入口 =================
|
||||
if __name__ == "__main__":
|
||||
print("请选择操作:")
|
||||
@@ -124,11 +150,13 @@ if __name__ == "__main__":
|
||||
try:
|
||||
idx_choice = int(input("\n请选择要恢复的备份编号: ")) - 1
|
||||
if 0 <= idx_choice < len(backups):
|
||||
selected_backup = os.path.join(BACKUP_DIR_ROOT, backups[idx_choice])
|
||||
selected_backup = os.path.join(
|
||||
BACKUP_DIR_ROOT, backups[idx_choice]
|
||||
)
|
||||
restore_mongo(selected_backup)
|
||||
else:
|
||||
print("[-] 无效的选择。")
|
||||
except ValueError:
|
||||
print("[-] 请输入数字。")
|
||||
else:
|
||||
print("[-] 无效输入,退出。")
|
||||
print("[-] 无效输入,退出。")
|
||||
|
||||
Reference in New Issue
Block a user