【问题标题】:How to write script for incremental backup in Ubuntu?如何在 Ubuntu 中编写增量备份脚本?
【发布时间】:2020-07-31 16:01:13
【问题描述】:

我想在 Ubuntu 中实现增量备份,所以我正在考虑从源和目标中查找所有文件的 md5sum,并检查是否有两个文件具有相同的 md5sum,然后将该文件保留在目标中,否则如果不同,则将文件从源复制到目录。 我正在考虑在 bash 中执行此操作 任何人都可以帮助我了解如何检查不同目录中两个文件的 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

【问题讨论】:

  • 你试过什么?从那里开始更容易。或者尝试rsnapshot - 当我发现我的自定义备份解决方案是,呃,缺乏时,我切换到那个。
  • 感谢您的回复,但我需要在没有任何实用程序的情况下实现..
  • 既然你要使用md5sum,也许你可以使用cmp。没有任何实用程序意味着您要从头开始编写自己的代码。
  • 它可以与不同文件夹中的两个 md5sum 一起使用吗?我也想为文件夹中的每个文件循环执行此操作
  • 应该可以,是的。到目前为止你尝试过什么?

标签: bash ubuntu backup


【解决方案1】:

这是reinventing the wheel 之类的东西。

为此目的编写了一些实用程序,仅举几例

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 source/ -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 destination/ -type f -print0)
  • 对于带有空格、制表符和换行符的文件应该足够安全,但由于我没有带有换行符的文件,所以我真的不能说。

  • 根据您要执行的操作更改 if-else 语句中的操作。

  • 好吧,也许sha512sum有点过头了,把它改成md5sum

  • 在 shebang 之后添加set -x 以查看实际执行的内容,祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 2015-03-06
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    相关资源
    最近更新 更多