I have two Hestia instances the Cloud (ACTIVE) and the Primary (BACKUP). On the Primary instance, I have the PULL bash scripts, which pull the user’s data from the Cloud:
1.) Go to the Cloud and make v-backup-users backup
2.) Using for loop copy the .tar latest backups (exclude admin) from Cloud to Primary
3.) Using for loop restore via v-restore-user
4.) delete backups older than 1 day
All is working perfectly, but I noticed that my PRIMARY Hestia has bigger DISK usage since, for example, the mails that are deleted on the CLOUD, after restoring on the PRIMARY are not deleted, just new files are added + existing overwritten.
How can I solve this without using rysync --delete ?
Before restoring users from backups, I use this script to extract the names of the email accounts from latest backups and if the email account exists in the backup and in the system, I use doveadm to purge the mail account.
This is the script:
#!/usr/bin/env bash
set -o pipefail
if [[ $EUID -ne 0 ]]; then
echo "Error: you must run the script as root" >&2
exit 1
fi
backup_dir="/backup"
backups=$(for i in $(find "$backup_dir" -type f -name "*tar" | cut -f1 -d '.' | sort -u); do
file="$(sed -E 's/^.*\///' <<<"$i" | cut -d '.' -f1)"
find "$backup_dir" -type f -name "$file.*tar" | sort -n | tail -n1
done)
for fbackup in $backups; do
user="$(echo "$fbackup" | cut -d '/' -f3 | cut -d '.' -f1)"
if accounts="$(tar tf "$fbackup" 2>/dev/null | grep 'accounts.tar')"; then
echo "Processing user $user"
for account in $accounts; do
domain="$(echo "$account" | cut -d'/' -f3)"
if grep 'zst$' <<<"$account" &>/dev/null; then
ztype='--zstd'
else
ztype='--gzip'
fi
if ! uacc="$(tar xOf "$fbackup" "$account" | tar t "$ztype" 2>/dev/null | grep '/$' | cut -d '/' -f1 | sort -u)"; then
continue
fi
for email_user in $uacc; do
echo "Purging $email_user@$domain"
if [[ -d "/home/$user/mail/$domain/$email_user" ]]; then
echo "doveadm purge -u $email_user@$domain"
echo "doveadm expunge -u $email_user@$domain mailbox \"*\" all "
else
echo "User $email_user does not exist"
fi
echo
done
done
fi
done
As the script will purge all the mails of existing accounts, in above script it won’t run the doveadm commands, it will only show the commands that it would use. If you try the script and it shows the right accounts to be purged, edit the script and replace this: