从主 VPS1 到子 VPS2 进行 Rsync Webmin

从主 VPS1 到子 VPS2 进行 Rsync Webmin

我正在尝试复制(rsync)等/webmin目录从主 VPS (VPS-1) 复制到子 VPS (VPS-2)。但是,执行复制后,子 VPS 上的服务器摘要仍显示主 VPS 的 IP 地址。我的目标是保留子 VPS 的 IP 地址和网络配置,同时仅复制 Webmin 配置。我使用的是 Ubuntu,下面是我用来实现此目的的脚本:


# Create a timestamp for the log file
timestamp=$(date +%Y%m%d_%H%M%S)

# Log file path
log_file="/root/sync_log_${timestamp}.log"

# Function to log messages to the log file
log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$log_file"
}

# Log start of the synchronization
log "Starting synchronization..."

# Sync Webmin configuration files
log "Syncing Webmin configuration files..."
rsync -avz --delete --exclude='dns' --exclude='mail' --exclude='proc' --exclude='run' --exclude='sys' --exclude='tmp' --exclude='dev' --exclude='mnt' --exclude='var/run' --exclude='var/lock' --exclude='var/tmp' --exclude='var/log' /etc/webmin/ root@VPS-2-SERVER-IP:/etc/webmin/ >> "$log_file" 2>&1

# Dump all databases on VPS-1
log "Dumping all databases on VPS-1..."
mysqldump -u root -p'VPS-1_MYSQL_PASSWORD_FOR_USER_ROOT' --all-databases > /root/all_databases_dump.sql 2>> "$log_file"

# Copy the database dump to VPS-2
log "Copying the database dump to VPS-2..."
scp /root/all_databases_dump.sql root@VPS-2_SERVER_IP:/root/ >> "$log_file" 2>&1

# Import all databases on VPS-2 (no password needed for 'root' user)
log "Importing all databases on VPS-2..."
ssh root@VPS_2_SERVER_IP "mysql -u root -p'VPS_2_MYSQL_PASSWORD_FOR_USER_ROOT' < /root/all_databases_dump.sql" >> "$log_file" 2>&1

# Remove the local database dump file on VPS-1
log "Removing the local database dump file..."
rm /root/all_databases_dump.sql

# Log end of the synchronization
log "Synchronization completed."

# Optional: Delete old log files (older than 7 days)
# This will delete log files older than 7 days (adjust as needed)
find /root -maxdepth 1 -type f -name "sync_log_*" -mtime +7 -exec rm {} \;

# Exit with success status
exit 0```

相关内容