Problem with FTP backup rotation

Hello,
Recently, we noticed that Hestia deletes the newest backup instead of the oldest.

We made a test script using functions from /usr/local/hestia/func/backup.sh and /usr/local/hestia/bin/v-backup-user

Script:

#!/bin/bash

user=user-skin
source /etc/hestiacp/hestia.conf
source $HESTIA/func/main.sh
source $HESTIA/func/domain.sh
source $HESTIA/func/db.sh
source $HESTIA/func/backup.sh
source_conf "$HESTIA/conf/hestia.conf"
source_conf "/usr/local/hestia/data/users/user-skin/user.conf"
source_conf "/usr/local/hestia/conf/ftp.backup.conf"

ftpc "ls" | awk '{print $9}' | grep "^user-skin\." | grep ".tar"

backup_list=$(ftpc "ls" | awk '{print $9}' | grep "^user-skin\." | grep ".tar")

backups_count=$(echo "$backup_list" | wc -l)

if [ "$backups_count" -ge "$BACKUPS" ]; then
        backups_rm_number=$((backups_count - BACKUPS + 1))
        for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
                backup_date=$(echo $backup | sed -e "s/user-skin.//" -e "s/.tar$//")
                echo "Remove: $backup_date"
        done
fi

Result:

user-skin.2025-06-06_05-16-40.tar
user-skin.2025-05-16_05-17-08.tar
user-skin.2025-05-18_05-15-46.tar
user-skin.2025-06-03_05-17-54.tar
user-skin.2025-06-04_05-16-57.tar
user-skin.2025-05-26_05-15-28.tar
user-skin.2025-06-05_05-17-30.tar
user-skin.2025-05-19_05-16-28.tar
Remove: 2025-06-06_05-16-40

And as you can see, it deletes the newest backup and the sorting is incorrect.

Can you help us with this?

Hi @pinedcloud.com

Hestia deletes the backups that are listed first when the ls command is executed on your FTP server.

Usually, FTP servers list files sorted by name, but it looks like yours isn’t doing that.

user-skin.2025-06-06_05-16-40.tar
user-skin.2025-05-16_05-17-08.tar
user-skin.2025-05-18_05-15-46.tar
user-skin.2025-06-03_05-17-54.tar
user-skin.2025-06-04_05-16-57.tar
user-skin.2025-05-26_05-15-28.tar
user-skin.2025-06-05_05-17-30.tar
user-skin.2025-05-19_05-16-28.tar

But usually it should look like this:

user-skin.2025-05-16_05-17-08.tar
user-skin.2025-05-18_05-15-46.tar
user-skin.2025-05-19_05-16-28.tar
user-skin.2025-05-26_05-15-28.tar
user-skin.2025-06-03_05-17-54.tar
user-skin.2025-06-04_05-16-57.tar
user-skin.2025-06-05_05-17-30.tar
user-skin.2025-06-06_05-16-40.tar

And the removed file should be user-skin.2025-05-16_05-17-08.tar

What’s your FTP server?

You could try this fix:

sed -E -i.original 's/grep \"\.tar\"\)$/grep \"\.tar\" | sort\)/' /usr/local/hestia/func/backup.sh 

This will create a backup /usr/local/hestia/func/backup.sh.original of file /usr/local/hestia/func/backup.sh and will add | sort to ls commands so the output will be sorted correctly.

SFTPGo 2.6.2

This helped.
Can this be submitted as a pull request?
I wouldn’t want every HestiaCP update to remove this fix.

Maybe someone else has the same problem

I’ll do it but I need to do a couple tests. Could you please open an issue?

Well, the way that ftp server shows the list is really strange, it has no sense.

user-skin.2025-06-06_05-16-40.tar
user-skin.2025-05-16_05-17-08.tar
user-skin.2025-05-18_05-15-46.tar
user-skin.2025-06-03_05-17-54.tar
user-skin.2025-06-04_05-16-57.tar
user-skin.2025-05-26_05-15-28.tar
user-skin.2025-06-05_05-17-30.tar
user-skin.2025-05-19_05-16-28.tar
1 Like

Issue has been created: [Bug] Problem with FTP backup rotation Ā· Issue #5017 Ā· hestiacp/hestiacp Ā· GitHub

1 Like

I’ve created this PR

4 Likes