我目前正在為 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
我使用的命令:Full-Backup
bash script.sh -m full
增加的
bash script.sh -m inc -p full
執行腳本根本沒有給出任何錯誤。正如我上面提到的,它似乎仍在復制所有檔案。這是我做的一些測驗。
du -chs 的輸出
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
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 輸出
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
I suspect that the ./ has something to do with that. I might be wrong, but it looks suspicious. Though when executing the same command again, the ./ are not in the log, probably because I did it on the same day, so it was overwriting in the /Backup/Inc/2021-11-20 Folder.
Let me know for more information. I have been trying around for a long time now. Maybe I am simply wrong and there are links made and disk space economized.
uj5u.com熱心網友回復:
我沒有閱讀整個代碼,因為主要問題似乎并不存在。
驗證您的磁盤使用情況/Backups與目錄du -sh /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 -l
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
與 du -sh
du -sh /tmp/example/*
1.0M /tmp/example/data
1.0M /tmp/example/full
0 /tmp/example/incr
- 哦?有一個 1 MiB 的檔案
/tmp/example/incr但du錯過了它?
其實沒有。由于該檔案自上次備份(用 參考--link-dest)后沒有被修改,因此rsync創建了一個指向它的硬鏈接,而不是復制其內容。—硬鏈接將相同的記憶體空間連接到不同的檔案
并且du可以檢測硬鏈接并顯示真實的磁盤使用情況,但前提是在其引數中包含硬鏈接檔案(即使在子目錄中)。例如,如果您du -sh獨立使用for /tmp/example/incr:
du -sh /tmp/example/incr
1.0M /tmp/example/incr
- 您如何檢測到檔案存在硬鏈接?
ls -l 實際上向我們展示了它:
-rw-rw-r-- 2 user group 1048576 Nov 21 00:24 /tmp/example/full/zerofile
^
HERE
這個數字意味著有兩個現有的檔案硬鏈接:這個檔案本身和同一個檔案系統中的另一個。
關于你的代碼
它不會改變任何東西,但我會替換:
#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/
和:
#Get Latest Folder
glob='20[0-9][0-9]-[0-1][0-9]-[0-3][0-9]' # match a timestamp (more or less)
NewestBackup=$(compgen -G "/Backups/$Method/$glob/" | sort -nr | head -n 1)
glob確保找到的目錄/檔案compgen -G具有正確的格式。- 添加
/在 glob 的末尾確保它只匹配目錄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362992.html
上一篇:本地與提供的Shell腳本引數
