0

I create backup using rsync with the following two scripts:

Script 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

Script 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

The output looks like:

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

I want to delete older purple_rsync_bak and red_rsync_bak directories if sufficient space is not available to make the current backup (by running Script 1 or Script 2).

How can I do that?

Ahmad Ismail
  • 2,678

1 Answers1

0

I have not a clue how to get the size of the backup unless it is produced by your output of rsync in the dry-run. This below will be your space left on a partition as a number to be used in a variable for comparison to your desired space left before a deletion occurs. You would do the comparison in your case statement for the Yy before the rsync occurs a line above it doing the compare and delete if necessary.

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

Or as a percentage used.

root@buster-raspi:~# df -h | grep /dev/sda1 | cut -d " " -f15 | tr -d %
59