我目前正在为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
执行脚本根本不会产生任何错误。正如我上面提到的,它似乎还在复制所有的文件。这是我做过的一些测试。
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 totalls -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我怀疑./与此有关。我可能错了,但看起来很可疑。尽管当再次执行相同的命令时,./不在日志中,可能是因为我在同一天执行了该命令,因此它在/Backup/Inc/2021-11-20文件夹中被覆盖。
想了解更多信息,请告诉我。我已经尝试了很长时间了。也许我是完全错误的,有链接和节省磁盘空间。
发布于 2021-11-20 23:57:34
我没有看完整的代码,因为主要的问题似乎不在那里。
使用/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/tmp/example/incr中有一个1/tmp/example/incr文件,但是du错过了它?其实没有。由于文件在上次备份(用--link-dest引用)后没有修改,所以rsync创建了一个硬链接,而不是复制其内容。-硬链接将相同的内存空间连接到不同的文件
du可以检测硬链接并向您显示真正的磁盘使用情况,但是只有当硬链接文件包含在其参数中(甚至在子目录中)时,才能显示硬盘的实际使用情况。例如,如果您对du -sh单独使用/tmp/example/incr
du -sh /tmp/example/incr
1.0M /tmp/example/incrls -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末尾具有正确的format./,确保它只匹配目录。https://stackoverflow.com/questions/70049407
复制相似问题