![如果没有足够的空间来进行当前备份,rsync 会删除较旧的“--backup-dir”目录](https://linux22.com/image/187757/%E5%A6%82%E6%9E%9C%E6%B2%A1%E6%9C%89%E8%B6%B3%E5%A4%9F%E7%9A%84%E7%A9%BA%E9%97%B4%E6%9D%A5%E8%BF%9B%E8%A1%8C%E5%BD%93%E5%89%8D%E5%A4%87%E4%BB%BD%EF%BC%8Crsync%20%E4%BC%9A%E5%88%A0%E9%99%A4%E8%BE%83%E6%97%A7%E7%9A%84%E2%80%9C--backup-dir%E2%80%9D%E7%9B%AE%E5%BD%95.png)
我使用 rsync 和以下两个脚本创建备份:
脚本1:
#!/bin/bash
rsync --dry-run --stats -ab --human-readable --inplace --delete-delay --debug=NONE --log-file=/media/blueray/UltrastarDaily/rsync-WDPurple.log --backup-dir=purple_rsync_bak.$(date +"%d-%m-%y_%I-%M-%S%P") --log-file-format='%t %f %o %M' --exclude='lost+found' --exclude='.Trash-1000' /media/blueray/WDPurple /media/blueray/UltrastarDaily | sed -e '1,4d;6d;8,15d'
echo -e "\nDo you want to continue?"
while true; do
case $yn in
[Yy]* ) rsync --info=PROGRESS2,BACKUP,DEL -ab --human-readable --inplace --delete-delay --debug=NONE --log-file=/media/blueray/UltrastarDaily/rsync-WDPurple.log --backup-dir=purple_rsync_bak.$(date +"%d-%m-%y_%I-%M-%S%P") --log-file-format='%t %f %o %M' --exclude='lost+found' --exclude='.Trash-1000' /media/blueray/WDPurple /media/blueray/UltrastarDaily; break;;
[Nn]* ) exit;;
* ) read -p "Please answer yes or no: " yn;;
esac
done
脚本2:
#!/bin/bash
rsync --dry-run --stats -ab --human-readable --inplace --delete-delay --debug=NONE --log-file=/media/blueray/UltrastarDaily/rsync-WDRed.log --backup-dir=red_rsync_bak.$(date +"%d-%m-%y_%I-%M-%S%P") --log-file-format='%t %f %o %M' --exclude='lost+found' --exclude='.Trash-1000' /media/blueray/WDRed /media/blueray/UltrastarDaily | sed -e '1,4d;6d;8,15d'
echo -e "\nDo you want to continue?"
while true; do
case $yn in
[Yy]* ) rsync --info=PROGRESS2,BACKUP,DEL -ab --human-readable --inplace --delete-delay --debug=NONE --log-file=/media/blueray/UltrastarDaily/rsync-WDRed.log --backup-dir=red_rsync_bak.$(date +"%d-%m-%y_%I-%M-%S%P") --log-file-format='%t %f %o %M' --exclude='lost+found' --exclude='.Trash-1000' /media/blueray/WDRed /media/blueray/UltrastarDaily; break;;
[Nn]* ) exit;;
* ) read -p "Please answer yes or no: " yn;;
esac
done
输出看起来像:
UltrastarDaily% tree -L 1
.
├── lost+found
├── purple_rsync_bak.06-02-21_06-38-44am
├── purple_rsync_bak.06-02-21_07-41-32pm
├── purple_rsync_bak.07-02-21_08-02-51am
├── purple_rsync_bak.07-02-21_08-17-26am
├── purple_rsync_bak.08-02-21_02-00-06am
├── red_rsync_bak.01-02-21_06-11-39pm
├── red_rsync_bak.06-02-21_06-16-58am
├── red_rsync_bak.06-02-21_06-23-24am
├── red_rsync_bak.06-02-21_06-26-58am
├── red_rsync_bak.06-02-21_06-27-30am
├── red_rsync_bak.06-02-21_06-31-36am
├── red_rsync_bak.06-02-21_06-33-14am
├── red_rsync_bak.06-02-21_06-34-04am
├── red_rsync_bak.06-02-21_06-34-52am
├── red_rsync_bak.06-02-21_06-35-22am
├── red_rsync_bak.06-02-21_06-41-48am
├── red_rsync_bak.06-02-21_07-39-41pm
├── red_rsync_bak.07-02-21_08-01-14am
├── red_rsync_bak.07-02-21_08-17-41am
├── red_rsync_bak.07-02-21_08-38-52am
├── red_rsync_bak.08-02-21_01-56-43am
├── red_rsync_bak.27-01-21_06-13-39pm
├── red_rsync_bak.28-01-21_02-22-31pm
├── red_rsync_bak.30-01-21_12-48-03am
├── rsync-WDPurple.log
├── rsync-WDRed.log
├── WDPurple
└── WDRed
27 directories, 2 files
如果没有足够的空间来进行当前备份(通过运行或),我想删除旧目录purple_rsync_bak
和目录。red_rsync_bak
Script 1
Script 2
我怎样才能做到这一点?
答案1
我不知道如何获取备份的大小,除非它是由您在空运行中的 rsync 输出生成的。下面将是您在分区上剩余的空间,作为变量中使用的数字,以便与删除发生之前所需的剩余空间进行比较。您可以在 rsync 发生之前在 case 语句中对 Yy 进行比较,并在其上方进行比较并删除(如有必要)。
root@buster-raspi:~# df -h | grep /dev/sda1
/dev/sda1 200M 117M 83M 59% /boot/firmware
root@buster-raspi:~# df -h | grep /dev/sda1 | cut -d " " -f13
83M
root@buster-raspi:~# df -h | grep /dev/sda1 | cut -d " " -f13 | tr -d M
83
或以百分比形式使用。
root@buster-raspi:~# df -h | grep /dev/sda1 | cut -d " " -f15 | tr -d %
59