Automatically delete old on demand snapshots?

I created a pacman hook to automatically take a snapshot before an upgrade based on this post about the topic, but I did not realize timeshift does not have an option to only keep a selected number of on demand snapshots and now they're quickly filling up the partition I have set aside for timeshift.

How would I go about automating this process? I only want to keep maybe like 3 on demand snapshots.

My system.

Why not add to the hook then ?

(care to show the hook? and maybe a list of the snapshots? easier to create some bash syntax for it if I know hhow they are named)

Forgive, but I'm not sure what specifically to add in order to check how many snapshots there are and delete the oldest one if the number is surpassed.

care to show the hook? and maybe a list of the snapshots? easier to create some bash syntax for it if I know how they are named

Timeshift hook:

[Trigger]
Operation = Upgrade
Type = Package
Target = *

[Action]
Description = pre-upgrade timeshift snapshot
When = PreTransaction
Exec = /etc/pacman.d/hooks.bin/timeshift.sh

Timeshift script

#!/bin/bash
#/etc/pacman.d/hooks.bin/timeshift.sh
user="$1"
find /timeshift/timeshift/snapshots-ondemand -mmin -30 | grep $(date +%Y-%m-%d)
if [ $? -eq 0 ]; then
    echo timeshift backup aborted, time threshold not met
else
    /usr/bin/timeshift --create --comments "timeshift-pacman-hook-snapshot"
fi

Snapshots naming convention is YYYY-MM-DD_HH-MM-SS

If you use the other combined hook I wrote with a keyboard cancelation option they won't mount up so fast. I usually just do manual backup rather than automated. When there gets to be more than I want I simply delete the whole works and start from a fresh base.

1 Like

I saw that and was going to implement it if I couldn't figure out how to automate it or find someone smarter than me to help me do that.

I could be wrong, but I don't think you can selectively delete on demand snapsots if there are scheduled snaps shots taken in between. They all have to be deleted consecutively back to a point where they are again contiguous. I could be wrong? :man_shrugging:

Well I wouldn't mind deleting scheduled ones if that's what's blocking me.

Something like this would get all but the 3 most recent files:

ls -tp /timeshift/timeshift/snapshots-ondemand | grep -v '/$' | grep '[0-9][0-9]' | tail -n +3 

.. tell me.. how does that work?

1 Like

That does a good job of only showing the snapshots older than the last 3. I guess I would then pipe that into a rm -rf?

Also, would you happen to know about @tbg's comment of snapshots needing to be contiguous if I have scheduled snapshots as well?

Excuse me if I am misunderstanding something.

I don't pretend to know all the inner workings of TS, but I think this is backasswards. You need to delete all snapshots backwards in time consecutively, back to a certain point. You can't selectively delete snapshots earlier in the sequence as they are all linked consecutively.

The first snapshot base is linked to the next snap, etc, etc. You can only delete from the end in a consecutive manner backwards to a specific point. Then you can take new snapshots which will then be linked to the last snapshot which was not deleted.

(edit)

My understanding is you cannot just delete the ondemand snapshots if there are intervening scheduled snapshots. All snapshots must be treated the same in the base timeshift folder and be deleted consecutively backwards by timestamp regardless of wether they were ondemand or scheduled.

What you say makes sense to me. I wouldn't want to mess up my snapshots by deleting the wrong ones.

If you want to automate removal be sure to only test by renaming the snapshots first. Do not use rm until you know it is working to your liking.

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

Forum kindly sponsored by