0

I am using Arch Linux, with snapper and BTRFS.

I am writing a script for personal use to simplify recovering a system via snapper snapshots. One should obviously boot into live USB and mount the top level subvolume to /mnt first.

I followed the ArchWiki steps to list all snapshots snapper has created but I want to show both date and description. I feel I might need to rollback to a specific snapshot that I will only recognize by description rather than just date.

The Wiki suggests to run:

grep -r '<date>' /mnt/@snapshots/*/info.xml

Is there a way I can grep both date and description to appear in one line just like when you run snapper ls on a running system.

I guess the output should be something similar to the below

/mnt/@snapshots/21/info.xml:  <date>2021-07-20 22:00:00</date <description>pacman -Syu</description>
/mnt/@snapshots/24/info.xml:  <date>2021-07-22 22:00:00</date> <description>pacman -S tree</description>
/mnt/@snapshots/25/info.xml:  <date>2021-07-23 22:00:00</date> <description>binutils boost-libs breezy clucene cmake copyq exiv2 gedit gnome-desktop</description>
/mnt/@snapshots/26/info.xml:  <date>2021-07-24 22:00:00</date> <description>google-chrome</description>

1
  • Can you post an example of exactly how you want the output to look? Add to your question, please. Mar 1, 2023 at 16:53

1 Answer 1

0

Since you're dealing with xml files, you should use an xml parser like xmlstarlet. Consider this xml file:

$ sudo cat /.snapshots/2831/info.xml
<?xml version="1.0"?>
<snapshot>
  <type>pre</type>
  <num>2831</num>
  <date>2023-02-28 17:07:56</date>
  <description>pacman -S -u --config /etc/pacman.conf -- endeavouros/eos-translations e</description>
  <cleanup>number</cleanup>
</snapshot>

This example will pull the description from a snapshot:

$ sudo xmlstarlet select --template --value-of /snapshot/description /.snapshots/2831/info.xml
pacman -S -u --config /etc/pacman.conf -- endeavouros/eos-translations e

Change the --value-of entry to get other values. For example, to get the date, use --value-of /snapshot/date. Now you can assign variables to the results and display them as required.

Now, since xmlstarlet is not available on the standard Arch ISO, I'll show an example with grep (NOTE: this will need to be run at root):

#!/usr/bin/env bash

SNAPSHOT_DIR="/.snapshots"
export MYFILES=$(ls "$SNAPSHOT_DIR"/*/info.xml)

for name in $MYFILES
do
    echo $name: $(grep -r '<date>' $name) $(grep -r '<description>' $name)
done

Sample output:

/.snapshots/2836/info.xml: <date>2023-02-28 17:45:05</date> <description>pacman -R -c -s --config /etc/pacman.conf -- linux-lts515 linux-lts515-h</description>
/.snapshots/2837/info.xml: <date>2023-02-28 17:45:06</date> <description>linux-lts515 linux-lts515-headers</description>
/.snapshots/2838/info.xml: <date>2023-02-28 17:51:52</date> <description>pacman -R -c -s --config /etc/pacman.conf -- chaotic-keyring chaotic-mir</description>
/.snapshots/2839/info.xml: <date>2023-02-28 17:51:53</date> <description>chaotic-keyring chaotic-mirrorlist</description>
/.snapshots/2855/info.xml: <date>2023-03-01 08:57:46</date> <description>timeline</description>
/.snapshots/2866/info.xml: <date>2023-03-01 20:08:03</date> <description>pacman -S -u --config /etc/pacman.conf -- endeavouros/eos-translations e</description>
/.snapshots/2867/info.xml: <date>2023-03-01 20:08:08</date> <description>babl dbus-python eos-translations ethtool ffmpeg fuse-common fuse3 gst-l</description>
/.snapshots/2868/info.xml: <date>2023-03-01 20:08:18</date> <description>pacman -U --noconfirm --config /etc/pacman.conf -- /home/ajgringo619/.ca</description>
/.snapshots/2869/info.xml: <date>2023-03-01 20:08:19</date> <description>pandoc-bin</description>
/.snapshots/2879/info.xml: <date>2023-03-02 05:08:38</date> <description>pacman -U --noconfirm --config /etc/pacman.conf -- /home/ajgringo619/.ca</description>
/.snapshots/2880/info.xml: <date>2023-03-02 05:09:39</date> <description>linux-lts-515-git linux-lts-515-git-headers</description>
4
  • What I need is to list all the snapshots with dates and descriptions and should be able to select which one I want to rollback to. This also needs installing xmlstartlet because it will not be installed on an archiso USB.
    – Muzzamil
    Mar 1, 2023 at 17:30
  • In that case, you build a custom ISO (with archiso) that does have xmlstarlet available. If you'd rather stay with the stock Arch ISO, then I'll add a grep section to my anwser. Mar 1, 2023 at 18:27
  • Let me know if this works. It should be easy to create variables with the data. Mar 3, 2023 at 2:00
  • Sorry fo rthe delay in reply, I was busy and did not get the chance to try it. It worked! You made my day man thank you. Although the output list for me is not order but that is not really a big deal. I just changed the SNAPSHOT_DIR="/.snapshots" to "/mnt/@snapshots" as this is where it will mounted as per Archwiki and as per my script.
    – Muzzamil
    Mar 4, 2023 at 14:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .