# Problem with FTP backup rotation

**URL:** <https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189>\
**Category:** Backups\
**Created:** [June 6, 2025, 10:33pm UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189 "2025-06-06T22:33:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pinedcloud.com](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/p/8edcca/32.png) [@pinedcloud.com](https://forum.hestiacp.com/u/pinedcloud.com)\
**Post date:** [June 6, 2025, 10:33pm UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/1 "2025-06-06T22:33:24Z")

</div>

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:

```auto
#!/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?

---

<div class="post-metadata">

**Author:** ![sahsanu](https://forum.hestiacp.com/user_avatar/forum.hestiacp.com/sahsanu/32/9318_2.png) [@sahsanu](https://forum.hestiacp.com/u/sahsanu)\
**Post date:** [June 7, 2025, 12:13am UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/2 "2025-06-07T00:13:58Z")

</div>

Hi @pinedcloud.com

> [@pinedcloud.com](#):
>
> Hestia deletes the newest backup instead of the oldest.

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.

```t
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:

```t
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?

---

<div class="post-metadata">

**Author:** ![sahsanu](https://forum.hestiacp.com/user_avatar/forum.hestiacp.com/sahsanu/32/9318_2.png) [@sahsanu](https://forum.hestiacp.com/u/sahsanu)\
**Post date:** [June 7, 2025, 12:32am UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/3 "2025-06-07T00:32:00Z")

</div>

You could try this fix:

```t
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.

---

<div class="post-metadata">

**Author:** ![pinedcloud.com](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/p/8edcca/32.png) [@pinedcloud.com](https://forum.hestiacp.com/u/pinedcloud.com)\
**Post date:** [June 7, 2025, 8:39am UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/4 "2025-06-07T08:39:49Z")

</div>

> [@sahsanu](#):
>
> What’s your FTP server?

SFTPGo 2.6.2

---

<div class="post-metadata">

**Author:** ![pinedcloud.com](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/p/8edcca/32.png) [@pinedcloud.com](https://forum.hestiacp.com/u/pinedcloud.com)\
**Post date:** [June 7, 2025, 10:10am UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/5 "2025-06-07T10:10:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sahsanu](https://forum.hestiacp.com/user_avatar/forum.hestiacp.com/sahsanu/32/9318_2.png) [@sahsanu](https://forum.hestiacp.com/u/sahsanu)\
**Post date:** [June 7, 2025, 10:36am UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/6 "2025-06-07T10:36:19Z")

</div>

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

> [@pinedcloud.com](#):
>
> SFTPGo 2.6.2

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

```t
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

```

---

<div class="post-metadata">

**Author:** ![pinedcloud.com](https://forum.hestiacp.com/letter_avatar_proxy/v4/letter/p/8edcca/32.png) [@pinedcloud.com](https://forum.hestiacp.com/u/pinedcloud.com)\
**Post date:** [June 7, 2025, 10:55am UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/7 "2025-06-07T10:55:11Z")

</div>

Issue has been created: [[Bug] Problem with FTP backup rotation · Issue #5017 · hestiacp/hestiacp · GitHub](https://github.com/hestiacp/hestiacp/issues/5017)

---

<div class="post-metadata">

**Author:** ![sahsanu](https://forum.hestiacp.com/user_avatar/forum.hestiacp.com/sahsanu/32/9318_2.png) [@sahsanu](https://forum.hestiacp.com/u/sahsanu)\
**Post date:** [June 7, 2025, 12:08pm UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/8 "2025-06-07T12:08:52Z")

</div>

I’ve created this PR

> <https://github.com/hestiacp/hestiacp/pull/5018>
>
> Fixes #5017

---

<div class="post-metadata">

**Author:** ![system](https://forum.hestiacp.com/uploads/default/original/1X/d9a5b68bf50ced83e880b56452e592b204761b19.png) [@system](https://forum.hestiacp.com/u/system)\
**Post date:** [July 7, 2025, 12:09pm UTC](https://forum.hestiacp.com/t/problem-with-ftp-backup-rotation/19189/9 "2025-07-07T12:09:10Z")

</div>

Closed on July 7, 2025, 12:09pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
