#!/bin/bash # 定义备份目录 backupDir='/home/merge/test' # 定义变量 userCollection='user' guildCollection='guildInfo' host='172.16.16.229' port=8635 # 定义目标和源的schema列表 targetSchema=("x1_bt_game_30001" "x1_bt_game_30002") sourceSchema=("x1_bt_game_20162&x1_bt_game_20163" "x1_bt_game_20165&x1_bt_game_20166&x1_bt_game_20167") # 遍历目标schema和对应的源schema 备份数据 for index in "${!targetSchema[@]}" do target="${targetSchema[$index]}" sources="${sourceSchema[$index]}" # 将源schema列表分割为单独的元素 IFS='&' read -r -a sourceArray <<< "$sources" : # 备份数据 for source in "${sourceArray[@]}" do # 使用mongodump命令备份数据 mongodump --host "$host" --port $port --db "$source" --collection "$userCollection" -u rwuser -p '4r7BU31I46G4!' --authenticationDatabase admin --out "$backupDir/$target" echo "备份 $userCollection 数据到 $backupDir/$target/$source" mongodump --host "$host" --port $port --db "$source" --collection "$guildCollection" -u rwuser -p '4r7BU31I46G4!' --authenticationDatabase admin --out "$backupDir/$target" echo "备份 $guildCollection 数据到 $backupDir/$target/$source" done done # 遍历目标schema和对应的源schema 合并数据 for index in "${!targetSchema[@]}" do target="${targetSchema[$index]}" sources="${sourceSchema[$index]}" # 将源schema列表分割为单独的元素 IFS='&' read -r -a sourceArray <<< "$sources" for source in "${sourceArray[@]}" do # 使用mongorestore命令合并数据到目标schema mongorestore --host "$host" --port $port --db "$target" --collection "$userCollection" -u rwuser -p '4r7BU31I46G4!' --authenticationDatabase admin "$backupDir/$target/$source/$userCollection.bson" echo "合并 $source.$userCollection 数据到 $target" mongorestore --host "$host" --port $port --db "$target" --collection "$guildCollection" -u rwuser -p '4r7BU31I46G4!' --authenticationDatabase admin "$backupDir/$target/$source/$guildCollection.bson" echo "合并 $source.$guildCollection 数据到 $target" done done