簡體   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