已解决
两台主机间文件同步
来自网友在路上 178878提问 提问时间:2023-09-20 14:45:15阅读次数: 78
最佳答案 问答题库788位专家为你答疑解惑
说明:需要同步A主机上的一个文件到B主机上。
#!/bin/bash# Function to synchronize, modify, and compare files
sync_modify_compare() {local source_folder="$1"local file="$2"local destination_host="$3"local destination_folder="$4"# Get MD5 hash of the source server filesource_md5=$(md5sum "$source_folder/$file" | cut -d ' ' -f 1)# Sync the file to the destination serverrsync -avzv "$source_folder/$file" "$destination_host:$destination_folder/" || {echo "Rsync failed."exit 1}# Get MD5 hash of the destination server filedestination_md5=$(ssh "$destination_host" "md5sum '$destination_folder/$file'" | cut -d ' ' -f 1)# Compare MD5 hashes of the source and destination server filesif [ "$source_md5" == "$destination_md5" ]; thenecho "File MD5 hashes match, synchronization successful."elseecho "File MD5 hashes do not match, synchronization failed."exit 1fi# Get content of the file from the destination servercontent_b=$(ssh "$destination_host" "cat '$destination_folder/$file'" 2>&1)exit_status_b=$?if [ $exit_status_b -ne 0 ]; thenecho "Unable to retrieve content of the file on the destination server."exit 1fi# Save content of the file from the destination server to a temporary filetemp_file=$(mktemp)echo "$content_b" > "$temp_file"# Use the diff command to compare file contentsdiff_output=$(diff "$source_folder/$file" "$temp_file")if [ -z "$diff_output" ]; thenecho "File contents are identical on both hosts."elseecho "File contents differ between the two hosts:"echo "$diff_output"fi# Remove the temporary filerm "$temp_file"
}# Example usage:
# sync_modify_compare "/path/to/source" "file.txt" "user@destination_host" "/path/to/destination"
查看全文
99%的人还看了
相似问题
- docker容器内访问主机端口服务
- 大数据-之LibrA数据库系统告警处理(ALM-12053 主机文件句柄使用率超过阈值)
- 如何让VirtualBox系统使用Ubuntu主机的USB
- 独立IP主机怎么样?对网站有什么影响
- OpenStack使用SPICE替换VNC——宿主机Ubuntu22.04
- Ubuntu20.04换源教程、解决主机与虚拟机之间进行文本复制粘贴问题
- 【已解决】设置SSH主机:VS Code-正在本地下载 VS Code 服务器
- linux的shell script判断用户输入的字符串,判断主机端口开通情况
- [已解决]该主机与 Cloudera Manager Server 失去联系的时间过长。 该主机未与 Host Monitor 建立联系。
- VSCode实用远程主机功能
猜你感兴趣
版权申明
本文"两台主机间文件同步":http://eshow365.cn/6-10020-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 三、双指针(two-point)
- 下一篇: 爬虫项目(四):抓取网页所有图片