Home  >  Q&A  >  body text

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

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

比如

~/dock1 ~/dock2

要保证完全一致 ...

软连接是不行的好像

伊谢尔伦伊谢尔伦2744 days ago1124

reply all(13)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 13:27:56

    rsync + inotify

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:27:56

    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
    

    reply
    0
  • 阿神

    阿神2017-04-17 13:27:56

    crontab+rsync

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:27:56

    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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:27:56

    Write a synchronization script and put it in crontab

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:27:56

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:27:56

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

    reply
    0
  • 阿神

    阿神2017-04-17 13:27:56

    For Dropbox, I used junction, just for links

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:27:56

    rsync is awesome

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:27:56

    rsync +1
    cron +1

    reply
    0
  • Cancelreply