#1041748 snapper: feature idea: configure debian snapper package to append apt command as snapshot description

Package:
snapper
Source:
snapper
Description:
Linux filesystem snapshot management tool
Submitter:
Anchal Nigam
Date:
2023-07-24 02:27:03 UTC
Severity:
normal
#1041748#5
Date:
2023-07-23 02:02:55 UTC
From:
To:
Dear Maintainer,

Right now, on `debian`, snapper will take pre/post snapshots when you use `apt`
and set the snapshot description to `apt`. I think this is handled in the
80snapper file.

I had a few ideas for feature enhancement:

1. If an inline variable is set in the `apt` command call, add that to the
description. For example: `SD="doing something funky" sudo apt install funky`
would set the snapshot description to `apt; doing something funky`.

2. Add the `apt` command line arguments to the description. Using my above
example, the description might be something like `apt; install funky; doing
something funky`.

3. Maybe there could be a way to force an interactive mode that would ask the
user what the snapshot description should be.

Just a thought.


*** Reporter, please consider answering these questions, where appropriate ***

   * What led up to the situation? With the current configuration, the system
does automatically take snaptshots but thhe descriptions aren't very helpful.
With this idea, the snapshot descriptions would be more helpful.
   * What exactly did you do (or not do) that was effective (or ineffective)?
N/A
   * What was the outcome of this action? N/A
   * What outcome did you expect instead? The full apt command should be added
to the snapshot description.

*** End of the template - remove these template lines ***

#1041748#10
Date:
2023-07-23 03:25:32 UTC
From:
To:
Dear Maintainer,

I was able to create my own script that does what I am talking about. This
script will use the apt command as the snapshot description.

I will try to submit a pull request into
https://salsa.debian.org/debian/snapper with my enhancements.

`**/etc/apt/apt.conf.d/80snapper**`:

```
DPkg::Pre-Invoke { "/path/to/dpkg-pre-post-snapper.sh pre"; };
DPkg::Pre-Invoke { "/path/to/dpkg-pre-post-snapper.sh post"; };
```

`**/path/to/dpkg-pre-post-snapper.sh**`

```
#!/bin/bash

# we need to work up the process tree to find the apt command that triggered
the call to this script
# get the initial PPID
PARENT_PID=${PPID}
# trim leading spacess
PARENT_PID="${PARENT_PID## }"

# if the command for this PPID is not apt
while [ "$(ps -ho comm "${PARENT_PID}")" != "apt" ] ; do
    # go up one level
    PARENT_PID=$(ps -ho ppid "${PARENT_PID}")
    PARENT_PID="${PARENT_PID## }"                             # trim leading
spacess
done

APT_CMD="$(ps -ho args "${PARENT_PID}")"

SNAPPER_DESCRIPTION="apt; ${APT_CMD}"

# source /etc/default/snapper if it exists
if [ -e /etc/default/snapper ] ; then
    . /etc/default/snapper
fi

# what action are we taking
if [ "$1" = "pre" ] ; then
    # pre, so take a pre snapshot

    # if snapper is installed
    # and if snapper snapshots are not being disabled using the
DISABLE_APT_SNAPSHOT variable
    # and if /etc/snapper/configs/root exists
    if [ -x /usr/bin/snapper ] && [ ! x$DISABLE_APT_SNAPSHOT = 'xyes' ] && [ -e
/etc/snapper/configs/root ] ; then
        # delete any lingering temp files
        rm -f /var/tmp/snapper-apt || true

        # create a snapshot
        # and save the snapshot number for reference later
        snapper create -d "${SNAPPER_DESCRIPTION}" -c number -t pre -p >
/var/tmp/snapper-apt || true

        # clean up snapper
        snapper cleanup number || true
    fi
elif [ "$1" = "post" ] ; then
    # post, so take a post snapshot

    # if snapper is installed
    # and if snapper snapshots are not being disabled using the
DISABLE_APT_SNAPSHOT variable
    # and if the temp file with the snapshot number from the pre snapshot
exists
    if [ -x /usr/bin/snapper ] && [ ! x$DISABLE_APT_SNAPSHOT = 'xyes' ] && [ -e
/var/tmp/snapper-apt ]
    then
        # take a post snapshot and link it to the # of the pre snapshot
        snapper create -d "${SNAPPER_DESCRIPTION}" -c number -t post --pre-
number=`cat /var/tmp/snapper-apt` || true

        # clean up snapper
        snapper cleanup number || true
    fi
fi

```

#1041748#15
Date:
2023-07-24 02:22:06 UTC
From:
To:
Dear Maintainer,

I created a gist with the relevant code/changes.

https://gist.github.com/imthenachoman/f722f6d08dfb404fed2a3b2d83263118