Keep newest number of files

I use Nextcloud to save backups for my apps. As every file will be uploaded to Nextcloud, my folder keeps getting bigger and bigger. I would like to keep only the latest two files in a folder.
I already saw that with the Retentions-App you can delete older files by date. But afaik there is no option to keep only the last x files in a folder?
I would appreciate any help with this topic!

That is a very simpel to resolve task.

You could run a litle script invoked by cron or inotify.

Something like this (I just wrote this draft I think it could work and haven’t tested it yet, so you’ll have to work on it):

#!/bin/bash

NEXTCLOUD_USER="www-data"
NEXTCLOUD_DIR="/var/www/nextcloud"
DATA_DIR="/var/www/nextcloud/data"

LOGFILE="/var/log/delete_old_files.log"
QUIET=false

# Parse options
while getopts "q" opt; do
    case $opt in
        q) QUIET=true ;;
        *) echo "Usage: $0 [-q] <user> <directory>" >&2; exit 1 ;;
    esac
done

shift $((OPTIND - 1))

# Check if a user is provided as an argument
if [ -z "$1" ]; then
    echo "Please provide a user."
    exit 1
fi

USER="$1"
shift

# Check if a directory is provided as an argument
if [ -z "$1" ]; then
    echo "Please provide a directory."
    exit 1
fi

DIR="$1"
ABS_DIR="$DATA_DIR/$USER/files/$DIR"

# Check if the specified directory exists
if [ ! -d "$ABS_DIR" ]; then
    echo "The specified directory does not exist."
    exit 1
fi

# Function to log output if -q is set
log() {
    if $QUIET; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOGFILE"
    else
        echo "$1"
    fi
}

# Find the two most recent files and delete the rest, count the number of files to delete
log "Starting cleanup in directory: $DIR of user $USER"
DELETED_FILES=$(find "$ABS_DIR" -type f -printf '%T@ %p\n' | sort -nr | awk 'NR>2 {print $2}')

if [ -n "$DELETED_FILES" ]; then
    # Delete the files if there are any to delete
    echo "$DELETED_FILES" | xargs -r rm
    log "Cleanup complete. Files deleted: $(echo "$DELETED_FILES" | wc -l)"

    # Trigger Nextcloud scan only if files were deleted
    sudo -u $NEXTCLOUD_USER php -f $NEXTCLOUD_DIR/occ files:scan --path=$USER/files/$DIR
    log "Nextcloud scan triggered for user $USER in directory $DIR."
else
    log "No files to delete. Skipping Nextcloud scan."
fi

Then you can invoke it either from the console:

/path/to/script.sh %user% %(/path/to)/directory%

or with the -q Flag from cron:

0 2 * * * /path/to/script.sh -q  %user% %(/path/to)/directory%

And of Course %user% should be replaced by the nextcloud user who owns the directory and %(/path/to)/directory% by the directory seen as from within your cloud.

hth.


Much and good luck,
ernolf

1 Like

Hi ernolf
Thank you very much for your help!
Unfortunately I use Storage Share from Hetzner and It seems like I cannot execute any scripts. :upside_down_face:
Is there an option to execute scripts from within Nextcloud?

I can’t imagine that because the whole server consists of scripts.
Or did you just mean that you don’t have console access?
How are your cron jobs triggered?

Yes, sure, that’s possible but it is not that easy.

One example:
With the app

App-Id files_scripts
App-Name File actions
Summary Scripting tool which allows administrators to expand the file options menu.
Categories files workflow
Repository GitHub - Raudius/files_scripts: Custom file actions app for Nextcloud
Issue-Tracker Issues · Raudius/files_scripts · GitHub
Admin-Doc. files_scripts/docs/README.md at master · Raudius/files_scripts · GitHub
PHP min/max 8.0 /
NC min/max 28 / 29
Not-shipped (not included) App available in appstore
Appstore File actions - Apps - App Store - Nextcloud

you could create the script in lua, that will be executed by the server. But the php-lua module has to be installed for that to happen. Since many distributions don’t have a packaged version of it, this could also fail for you.
On your own server or one with at least root access, you can easily build it yourself , but not if you are in a sublet.
There are probably other solutions, but I just can’t think of them right now. Most if not all of them need console- or even root access to the server.

What you can also do is not run the script on the server at all but on a sync client. A Windows or Linux client that synchronizes the directory. You will definitely be able to set up a small server at home that only serves to save you money on an expensive root server. This can then delete the older files according to your rules, which are then synchronized back as deleted.
Then you can also access your server directly via CLI. It just depends on your scripting skills.


Much and good luck,
ernolf

2 Likes

Of course, you are right :sweat_smile: I meant I have no console access.

This app looks really interesting, but the php-lua module is not installed and I guess I cannot install it myself…

That is a really good idea… I think this is the best option for me :+1:

Thank you for your help!
R0by

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