sync - Linux 如何保持两个文件夹内容一致
伊谢尔伦
伊谢尔伦 2017-04-17 13:25:56
0
13
1114

Linux 如何保持两个文件夹内容一致 ?

比如

~/dock1 ~/dock2

要保证完全一致 ...

软连接是不行的好像

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(13)
巴扎黑

rsync + inotify

黄舟

I think you should do automatic backup. If it is my own code files and the like, I usually back them up manually at critical times. It is also simple, just execute cp -r. There is a problem with automatic backup. I don’t know when to back up, for example, I backed up for you in 10 minutes, but actually you want to go back to 1 hour ago, so what should I do, so I still prefer manual backup, after all, it is simple.
In addition, if you just want to compare whether two folders are completely consistent, I wrote a tool

sh#!/usr/bin/env bash
# 比较两个文件夹是否一致
# 计算每个文件文件的md5值,将所有的md5值写入一个文件中,最后再用md5比较这个文件是否一样
# mac系统下计算md5的值命令是md5,如果是linux,请换成md5sum
# 用法:sh is_same.sh dir1 dir2

if [[ $# != 2 ]]; then
    echo "usage: sh is_same.sh <dir1> <dir2>"
    exit 1
fi

# find查找文件,md5计算md5值,awk取出md5值,sort保证次序是一致的,将结果输出到文件中
find  -type f -print | grep -v dir*.md5 | xargs md5 | awk '{print $NF}' | sort > temp.dir1.md5
find  -type f -print | grep -v dir*.md5 | xargs md5 | awk '{print $NF}' | sort > temp.dir2.md5

# 比较两个文件是否相同,如果相同说明两个目录是一致的
md5sum1=$(md5 temp.dir1.md5 | awk '{print $NF}')
md5sum2=$(md5 temp.dir2.md5 | awk '{print $NF}')

if [[ $md5sum1 != $md5sum2 ]]; then
    echo " and  is different"
else
    echo " and  is same"
fi

# 删除临时文件
rm temp.dir1.md5
rm temp.dir2.md5
阿神

crontab+rsync

洪涛

When both folders are local, if one is primary and the other is secondary, you should be able to use inotify + rsync

In addition, you can actually consider bit torrent sync or syncthing. Of course, this is mainly used when they are not on the same machine

大家讲道理

Write a synchronization script and put it in crontab

洪涛

If it is a file, you can use a hard link

巴扎黑

Actually, I just want to ask, what is this requirement for...

阿神

For Dropbox, I used junction, just for links

Ty80

rsync is awesome

洪涛

rsync +1
cron +1

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!