Skip to content

Instantly share code, notes, and snippets.

@harilvfs
Last active September 19, 2025 21:41
Show Gist options
  • Select an option

  • Save harilvfs/d8f1584206417482b6f6f9928d9af030 to your computer and use it in GitHub Desktop.

Select an option

Save harilvfs/d8f1584206417482b6f6f9928d9af030 to your computer and use it in GitHub Desktop.

Revisions

  1. harilvfs revised this gist Feb 11, 2025. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions TIMESHIFT.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    # Timeshift GRUB Boot Integration (EXT4 & BTRFS)

    This guide explains how to:
    This guide will show you how to:
    - Create and manage **Timeshift snapshots**
    - Boot into the latest **Timeshift snapshot** from **GRUB**
    - Boot into the latest Timeshift snapshot from GRUB
    - Automatically update the GRUB menu with new snapshots
    - Configure both **EXT4 (rsync)** and **BTRFS** setups
    - Handle both **`blkid` and non-`blkid` (Legacy BIOS/MBR) methods**
    - Set up Timeshift for both EXT4 (rsync) and BTRFS
    - Use both blkid and non-blkid (Legacy BIOS/MBR) methods

    ---

  2. harilvfs created this gist Feb 11, 2025.
    292 changes: 292 additions & 0 deletions TIMESHIFT.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,292 @@
    # Timeshift GRUB Boot Integration (EXT4 & BTRFS)

    This guide explains how to:
    - Create and manage **Timeshift snapshots**
    - Boot into the latest **Timeshift snapshot** from **GRUB**
    - Automatically update the GRUB menu with new snapshots
    - Configure both **EXT4 (rsync)** and **BTRFS** setups
    - Handle both **`blkid` and non-`blkid` (Legacy BIOS/MBR) methods**

    ---

    ## 🛠️ 1. Installing Timeshift
    Timeshift is a backup tool for Linux that supports **rsync** (EXT4) and **BTRFS** snapshots.

    > ```bash
    > # Install Timeshift on Arch-based systems
    > sudo pacman -S timeshift
    > ```
    > ```bash
    > # Install Timeshift on Debian/Ubuntu
    > sudo apt install timeshift -y
    > ```
    > ```bash
    > # Install Timeshift on Fedora
    > sudo dnf install timeshift -y
    > ```
    ---
    ## 📸 2. Creating a Timeshift Snapshot
    To create a snapshot **manually**:
    ```bash
    sudo timeshift --create --comments "Backup before update" --tags O
    ```
    To list available snapshots:
    ```bash
    sudo timeshift --list
    ```

    Example output:
    ```plaintext
    Device : /dev/sda3
    Snapshots:
    Num Name Tags Date
    ------------------------------------------
    0 2025-02-11_23-33-09 O 2025-02-11
    ```

    ---

    ## ⚙️ 3. Adding Timeshift to GRUB (EXT4 - Rsync)

    ### 🛠️ **Step 1: Find Your Snapshot Path**
    Find the latest snapshot:
    ```bash
    ls -1t /timeshift/snapshots | head -n 1
    ```

    Example path:
    ```plaintext
    /timeshift/snapshots/2025-02-11_23-33-09/localhost/boot
    ```

    ---

    ## 🔄 4. Automate GRUB Entry for Latest Snapshot

    ### **For Systems with `blkid` (Recommended)**
    Create `/usr/local/bin/update-grub-timeshift.sh`:
    ```bash
    sudo vim /usr/local/bin/update-grub-timeshift.sh
    ```

    Paste:
    ```bash
    #!/bin/bash

    SNAPSHOT_DIR="/timeshift/snapshots"
    LATEST_SNAPSHOT=$(ls -1t "$SNAPSHOT_DIR" | head -n 1)

    if [ -z "$LATEST_SNAPSHOT" ]; then
    echo "No Timeshift snapshots found!"
    exit 1
    fi

    BOOT_PATH="$SNAPSHOT_DIR/$LATEST_SNAPSHOT/localhost/boot"

    if [ ! -f "$BOOT_PATH/vmlinuz-linux" ] || [ ! -f "$BOOT_PATH/initramfs-linux.img" ]; then
    echo "Kernel or initramfs missing!"
    exit 1
    fi

    ROOT_UUID=$(blkid -s UUID -o value /dev/sda3)

    GRUB_CUSTOM="/etc/grub.d/40_custom"

    cat <<EOF > "$GRUB_CUSTOM"
    menuentry "Timeshift Recovery (Latest: $LATEST_SNAPSHOT)" {
    set root=(hd0,3)
    linux $BOOT_PATH/vmlinuz-linux root=UUID=$ROOT_UUID rw
    initrd $BOOT_PATH/initramfs-linux.img
    }
    EOF

    grub-mkconfig -o /boot/grub/grub.cfg
    ```

    Make executable:
    ```bash
    sudo chmod +x /usr/local/bin/update-grub-timeshift.sh
    ```

    Run manually:
    ```bash
    sudo /usr/local/bin/update-grub-timeshift.sh
    ```

    ---

    ### **For Legacy BIOS/MBR Systems (Non-`blkid` Method)**
    This script **avoids using UUIDs** and directly refers to the disk device.

    Edit the 40_custom file for Legacy BIOS:
    ```plaintext
    menuentry "Timeshift Recovery (Latest: $LATEST_SNAPSHOT)" {
    set root=(hd0,msdos3) # Adjust 'msdos3' based on your partition
    linux /timeshift/snapshots/2025-02-11_23-33-09/localhost/boot/vmlinuz-linux root=/dev/sda3 rw
    initrd /timeshift/snapshots/2025-02-11_23-33-09/localhost/boot/initramfs-linux.img
    }
    ```

    > [!IMPORTANT]
    > In the Legacy BIOS (MBR) setup, you use /dev/sda3 directly for root= instead of the UUID.
    After editing, save and update GRUB:

    ```bash
    sudo grub-mkconfig -o /boot/grub/grub.cfg
    ```

    Create `/usr/local/bin/update-grub-timeshift-legacy.sh`:
    ```bash
    sudo vim /usr/local/bin/update-grub-timeshift-legacy.sh
    ```

    Paste:
    ```bash
    #!/bin/bash

    SNAPSHOT_DIR="/timeshift/snapshots"
    LATEST_SNAPSHOT=$(ls -1t "$SNAPSHOT_DIR" | head -n 1)

    if [ -z "$LATEST_SNAPSHOT" ]; then
    echo "No Timeshift snapshots found!"
    exit 1
    fi

    BOOT_PATH="$SNAPSHOT_DIR/$LATEST_SNAPSHOT/localhost/boot"

    if [ ! -f "$BOOT_PATH/vmlinuz-linux" ] || [ ! -f "$BOOT_PATH/initramfs-linux.img" ]; then
    echo "Kernel or initramfs missing!"
    exit 1
    fi

    GRUB_CUSTOM="/etc/grub.d/40_custom"

    cat <<EOF > "$GRUB_CUSTOM"
    menuentry "Timeshift Recovery (Latest: $LATEST_SNAPSHOT)" {
    set root=(hd0,msdos3)
    linux $BOOT_PATH/vmlinuz-linux root=/dev/sda3 rw
    initrd $BOOT_PATH/initramfs-linux.img
    }
    EOF

    grub-mkconfig -o /boot/grub/grub.cfg
    ```

    Make executable:
    ```bash
    sudo chmod +x /usr/local/bin/update-grub-timeshift-legacy.sh
    ```

    Run manually:
    ```bash
    sudo /usr/local/bin/update-grub-timeshift-legacy.sh
    ```

    > [!IMPORTANT]
    > Use `root=/dev/sdX` instead of UUID if you have a **legacy MBR system**.
    ---

    ## 🕒 5. Automate via Systemd Timer
    Create `/etc/systemd/system/update-grub-timeshift.service`:
    ```bash
    sudo vim /etc/systemd/system/update-grub-timeshift.service
    ```

    Paste:
    ```plaintext
    [Unit]
    Description=Update GRUB with latest Timeshift snapshot
    After=timeshift.service
    [Service]
    ExecStart=/usr/local/bin/update-grub-timeshift.sh
    Type=oneshot
    ```

    Create `/etc/systemd/system/update-grub-timeshift.timer`:
    ```bash
    sudo nano /etc/systemd/system/update-grub-timeshift.timer
    ```

    Paste:
    ```plaintext
    [Unit]
    Description=Run GRUB update when Timeshift snapshot changes
    [Timer]
    OnCalendar=hourly
    Persistent=true
    [Install]
    WantedBy=timers.target
    ```

    Enable:
    ```bash
    sudo systemctl daemon-reload
    sudo systemctl enable --now update-grub-timeshift.timer
    ```

    ---

    ## 🖥️ 6. Adding Timeshift to GRUB (BTRFS)
    > [!IMPORTANT]
    > **BTRFS snapshots** use subvolumes instead of regular directories.
    Find the **default subvolume**:
    ```bash
    sudo btrfs subvolume list /
    ```

    To boot into a snapshot, use:
    ```plaintext
    menuentry "Timeshift BTRFS Recovery" {
    set root=(hd0,3)
    linux /@/boot/vmlinuz-linux root=UUID=<your-btrfs-uuid> rw rootflags=subvol=@
    initrd /@/boot/initramfs-linux.img
    }
    ```
    Replace `<your-btrfs-uuid>` with:
    ```bash
    blkid -s UUID -o value /dev/sda3
    ```

    Update GRUB:
    ```bash
    sudo grub-mkconfig -o /boot/grub/grub.cfg
    ```

    ---

    > [!CAUTION]
    > I do not recommend using the auto script if you are not familiar with shell scripting. It is safer to edit the GRUB configuration manually if you're unsure. This way, you can carefully check each step and avoid mistakes that could make your system unbootable.
    For UUID (Standard Method)

    Edit the 40_custom file for your UUID-based entry:
    ```bash
    sudo vim /etc/grub.d/40_custom
    ```

    Add:
    ```plaintext
    menuentry "Timeshift Recovery (Latest: $LATEST_SNAPSHOT)" {
    set root=(hd0,3)
    linux /timeshift/snapshots/2025-02-11_23-33-09/localhost/boot/vmlinuz-linux root=UUID=12345678-90ab-cdef-1234-567890abcdef rw
    initrd /timeshift/snapshots/2025-02-11_23-33-09/localhost/boot/initramfs-linux.img
    }
    ```

    Replace 12345678-90ab-cdef-1234-567890abcdef with the UUID of your root partition, which can be found with the blkid command.

    Then, update GRUB:
    ```bash
    sudo grub-mkconfig -o /boot/grub/grub.cfg
    ```