How do I keep the latest two files as backups? Will this script work?

What is the problem you are having with rclone?

I want to keep the latest two backups of proxmox

Run the command 'rclone version' and share the full output of the command.

root@rclone:~# rclone version
rclone v1.69.3

  • os/version: ubuntu 22.04 (64 bit)
  • os/kernel: 6.8.12-9-pve (x86_64)
  • os/type: linux
  • os/arch: amd64
  • go/version: go1.24.3
  • go/linking: static
  • go/tags: none

Which cloud storage system are you using? (eg Google Drive)

Mega

[proxmox-backups-mega]
type = mega
user = XXX
pass = XXX

Script

#!/bin/bash

# Proxmox backup script with rotation - keeps latest 2 backups
LOG_FILE="/var/log/rclone_backup.log"
LOCAL_DIR="/mnt/proxmox-backups"
REMOTE="proxmox-backups-mega:proxmox-backups"

echo "$(date): Starting backup process" >> "$LOG_FILE"

# Sync current backups
echo "$(date): Syncing backups to remote" >> "$LOG_FILE"
rclone sync "$LOCAL_DIR" "$REMOTE" -P >> "$LOG_FILE" 2>&1

if [ $? -eq 0 ]; then
    echo "$(date): Sync completed successfully" >> "$LOG_FILE"
    
    # Get list of files sorted by modification time (newest first)
    echo "$(date): Cleaning up old backups, keeping latest 2" >> "$LOG_FILE"
    
    # List files, sort by date, skip first 2, delete the rest
    rclone lsf "$REMOTE" --format pt | sort -k2 -r | tail -n +3 | while read file; do
        filename=$(echo "$file" | awk '{print $1}')
        echo "$(date): Deleting old backup: $filename" >> "$LOG_FILE"
        rclone delete "$REMOTE/$filename" >> "$LOG_FILE" 2>&1
    done
    
    echo "$(date): Cleanup completed" >> "$LOG_FILE"
else
    echo "$(date): Sync failed, skipping cleanup" >> "$LOG_FILE"
fi

echo "$(date): Backup process finished" >> "$LOG_FILE"
echo "----------------------------------------" >> "$LOG_FILE"

Or to keep it simpler, I could create two cron jobs?

I have my backups to run twice a week on Mon and Fri at 2am. This will sync my backups to the cloud on Mon and Fri at 6am.

0 6 * * 1,5 rclone sync /mnt/proxmox-backups proxmox-backups-mega:proxmox-backups -P >> /var/log/rclone_backup.log 2>&1

At 7am on Mon and Fri, this will delete the oldest backup leaving me with two backups? If it runs on 5/12/2025, 5/16/2025, and 5/19/2025. Then on 5/19/2025, my backup will complete. This will delete the backup created on 5/12/2025 leaving me with the latest two backups?

0 7 * * 1,5 rclone delete --min-age 8d proxmox-backups-mega:proxmox-backups >> /var/log/rclone_cleanup.log 2>&1

I only want to keep the back up created once a week on Monday and Friday

Backup created on Monday. Backup created on Friday. Backup created on Monday. Delete old Monday backup. Backup created on Friday. Delete old Friday backup.