繁体   English   中英

Rsync 增量备份仍然复制所有文件

[英]Rsync Incremental Backup still copies all the files

我目前正在为 rsync 编写 bash 脚本。 我很确定我做错了什么。 但我说不出它是什么。 我将尝试详细说明所有内容,希望有人可以帮助我。

脚本的目标是使用 rsync 进行完整备份和增量备份。 除了一件关键的事情外,一切似乎都运行良好。 似乎即使使用--link-dest参数,它仍然会复制所有文件。 我已经用du -chs检查了文件大小。

首先是我的脚本:

#!/bin/sh
while getopts m:p: flags
do
  case "$flags" in
    m) mode=${OPTARG};;
    p) prev=${OPTARG};;
    *) echo "usage: $0 [-m] [-p]" >&2
       exit 1 ;;
  esac
done

date="$(date '+%Y-%m-%d')";


#Create Folders If They Do Not Exist (-p paramter)
mkdir -p /Backups/Full && mkdir -p /Backups/Inc

FullBackup() {
  #Backup Content Of Website
  mkdir -p /Backups/Full/$date/Web/html
  rsync -av user@IP:/var/www/html/ /Backups/Full/$date/Web/html/

  #Backup All Config Files NEEDED. Saving Storage Is Key ;)
  mkdir -p /Backups/Full/$date/Web/etc
  rsync -av user@IP:/etc/apache2/ /Backups/Full/$date/Web/etc/

  #Backup Fileserver
  mkdir -p /Backups/Full/$date/Fileserver
  rsync -av user@IP:/srv/samba/private/ /Backups/Full/$date/Fileserver/

  #Backup MongoDB
  ssh user@IP /usr/bin/mongodump --out /home/DB
  rsync -av root@BackupServerIP:/home/DB/ /Backups/Full/$date/DB
  ssh user@IP rm -rf /home/DB
}

IncrementalBackup(){
  Method="";
  if [ "$prev" == "full" ]
  then
    Method="Full";
  elif [ "$prev" == "inc" ]
  then
    Method="Inc";
  fi

  if [ -z "$prev" ]
  then
  echo "-p Parameter Empty";
  else
  #Get Latest Folder - Ignore the hacky method, it works.
  cd /Backups/$Method
  NewestBackup=$(find . ! -path . -type d | sort -nr | head -1 | sed s@^./@@)
  IFS='/'
  read -a strarr <<< "$NewestBackup"
  Latest_Backup="${strarr[0]}";
  cd /Backups/

  #Incremental-Backup Content Of Website
  mkdir -p /Backups/Inc/$date/Web/html
  rsync -av --link-dest /Backups/$Method/"$Latest_Backup"/Web/html/ user@IP:/var/www/html/ /Backups/Inc/$date/Web/html/

  #Incremental-Backup All Config Files NEEDED
  mkdir -p /Backups/Inc/$date/Web/etc
  rsync -av --link-dest /Backups/$Method/"$Latest_Backup"/Web/etc/ user@IP:/etc/apache2/ /Backups/Inc/$date/Web/etc/

  #Incremental-Backup Fileserver
  mkdir -p /Backups/Inc/$date/Fileserver
  rsync -av --link-dest /Backups/$Method/"$Latest_Backup"/Fileserver/ user@IP:/srv/samba/private/ /Backups/Inc/$date/Fileserver/

  #Backup MongoDB
  ssh user@IP /usr/bin/mongodump --out /home/DB
  rsync -av root@BackupServerIP:/home/DB/ /Backups/Full/$date/DB
  ssh user@IP rm -rf /home/DB
  fi
}

if [ "$mode" == "full" ]
then
  FullBackup;
elif [ "$mode" == "inc" ]
then
  IncrementalBackup;
fi

我使用的命令:完整备份bash script.sh -m full

增量bash script.sh -m inc -p full

执行脚本根本没有给出任何错误。 正如我上面提到的,它似乎仍在复制所有文件。 这是我做的一些测试。

杜-chs的Output

root@Backup:/Backups# du -chs /Backups/Full/2021-11-20/*
36K     /Backups/Full/2021-11-20/DB
6.5M    /Backups/Full/2021-11-20/Fileserver
696K    /Backups/Full/2021-11-20/Web
7.2M    total
root@Backup:/Backups# du -chs /Backups/Inc/2021-11-20/*
36K     /Backups/Inc/2021-11-20/DB
6.5M    /Backups/Inc/2021-11-20/Fileserver
696K    /Backups/Inc/2021-11-20/Web
7.2M    total

Output of ls -li

root@Backup:/Backups# ls -li /Backups/Full/2021-11-20/
total 12
1290476 drwxr-xr-x 4 root root 4096 Nov 20 19:26 DB
1290445 drwxrwxr-x 6 root root 4096 Nov 20 18:54 Fileserver
1290246 drwxr-xr-x 4 root root 4096 Nov 20 19:26 Web
root@Backup:/Backups# ls -li /Backups/Inc/2021-11-20/
total 12
1290506 drwxr-xr-x 4 root root 4096 Nov 20 19:28 DB
1290496 drwxrwxr-x 6 root root 4096 Nov 20 18:54 Fileserver
1290486 drwxr-xr-x 4 root root 4096 Nov 20 19:28 Web

进行增量备份和更改/添加文件时的 Rsync Output

receiving incremental file list
./
lol.html

sent 53 bytes  received 194 bytes  164.67 bytes/sec
total size is 606  speedup is 2.45
receiving incremental file list
./

sent 33 bytes  received 5,468 bytes  11,002.00 bytes/sec
total size is 93,851  speedup is 17.06
receiving incremental file list
./

sent 36 bytes  received 1,105 bytes  760.67 bytes/sec
total size is 6,688,227  speedup is 5,861.72
*Irrelevant MongoDB Dump Text*

sent 146 bytes  received 2,671 bytes  1,878.00 bytes/sec
total size is 2,163  speedup is 0.77

我怀疑./与此有关。 我可能错了,但看起来很可疑。 虽然再次执行相同的命令时,./ 不在日志中,可能是因为我是在同一天执行的,所以它在/Backup/Inc/2021-11-20 ./夹中被覆盖。

让我知道更多信息。 我已经尝试了很长时间了。 也许我完全错了,并且建立了链接并节省了磁盘空间。

我没有阅读整个代码,因为主要问题似乎不存在。
使用du -sh /Backups验证/Backups目录的总大小,然后将其与du -sh /Backups/Full + du -sh /Backups/Inc进行比较。

我会用一个小测试告诉你为什么:

创建一个包含 1 MiB 文件的目录:

mkdir -p /tmp/example/data

dd if=/dev/zero of=/tmp/example/data/zerofile bs=1M count=1

进行“完整”备份:

rsync -av /tmp/example/data/ /tmp/example/full

进行“增量”备份

rsync -av --link-dest=/tmp/example/full /tmp/example/data/ /tmp/example/incr

现在让我们看看我们用ls得到了什么:

ls -l /tmp/example/*
-rw-rw-r-- 1 user group 1048576 Nov 21 00:24 /tmp/example/data/zerofile
-rw-rw-r-- 2 user group 1048576 Nov 21 00:24 /tmp/example/full/zerofile
-rw-rw-r-- 2 user group 1048576 Nov 21 00:24 /tmp/example/incr/zerofile

这里没问题,三个文件,每个文件 1 MiB。

现在与du

du -sh /tmp/example/data
1.0M    /tmp/example/data

du -sh /tmp/example/full
1.0M    /tmp/example/full

du -sh /tmp/example/incr
1.0M    /tmp/example/incr

du -sh /tmp/example
2.0M    /tmp/example

哦? 1 MiB + 1 MiB + 1 MiB 等于 2 MiB?

由于自上次备份(使用--link-dest引用)以来该文件没有被修改, rsync创建了一个指向该文件的硬链接,而不是复制其内容。 硬链接将文件连接到相同的 memory 空间

如何检测硬链接?

ls -l可以给你看:

-rw-rw-r-- 2 user group 1048576 Nov 21 00:24 /tmp/example/full/zerofile
           ^
          HERE

此数字表示该文件有两个现有的硬链接。


结论

使用du验证/Backups目录的总大小,并将其与/Backups/Full + /Backups/Inc的大小进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM