我想在Ubuntu中实现增量备份,所以我正在考虑从源和目标中查找所有文件的md5sum,并检查是否有两个文件具有相同的md5sum,然后将该文件保留在目标位置,否则将文件从源复制到目录中。 。我正在考虑这样做谁能帮助我检查不同目录下两个文件的md5sum命令?预先感谢!
#!/bin/bash
#
SOURCE="/home/pallavi/backup1"
DEST="/home/pallavi/BK"
count=1
TODAY=$(date +%F_%H%M%S)
cd "${DEST}" || exit 1
mkdir "${TODAY}"
while [ $count -le 1 ]; do
count=$(( $count + 1 ))
cp -R $SOURCE/* $DEST/$TODAY
mkdir "MD5"
cd ${DEST}/${TODAY}
for f in *;do
md5sum "${f}" >"${TODAY}${f}.md5"
echo ${f}
done
if [ $? -ne 0 ] && [[ $IGNORE_ERR -eq 0 ]]; then
#error or eof
echo "end of source or error"
break
fi
done
reinventing the wheel
之类的东西。有一些为此目的而编写的实用程序,用来命名
few。
rsync
GNUcp(1)
具有-u
标志。
cp
用于比较文件
cmp
diff
用于查找重复项
fdupes
rmlint
这是我想出的re-inventing the wheel
之类的东西。
#!/usr/bin/env bash shopt -s extglob declare -A source_array while IFS= read -r -d '' files; do read -r source_hash source_files < <(sha512sum "$files") source_array["$source_hash"]="$source_files" done < <(find source1/ -type f -print0) source=$( IFS='|'; printf '%s' "@(${!source_array[*]})" ) while IFS= read -r -d '' files0 ; do read -r destination_hash destination_files < <(sha512sum "$files0") if [[ $destination_hash == $source ]]; then echo "$destination_files" FOUND from source/ directory else echo "$destination_files" NOT-FOUND from source/ directory fi done < <(find destination1/ -type f -print0)
if-else
语句中的操作。 sha512sum
可能会被杀死,将其更改为md5sum
shebang后添加set -x
,以查看实际执行的操作,祝您好运。