如何在Ubuntu中编写用于增量备份的脚本?

问题描述 投票:0回答:1

我想在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
bash ubuntu backup
1个回答
0
投票
这是reinventing the wheel之类的东西。

有一些为此目的而编写的实用程序,用来命名

few。

rsync
GNU cp(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,以查看实际执行的操作,祝您好运。

© www.soinside.com 2019 - 2024. All rights reserved.