In the world of data management, ensuring that you have adequate backups while maintaining efficient use of storage is crucial. For users of Timeshift on Arch Linux, this balance can become challenging as snapshot data grows over time. In this blog post, we’ll explore an effective purge strategy to manage Timeshift snapshots, coupled with a detailed bash script to automate the process. This approach will help you retain essential backups while freeing up valuable storage space.


Why Backup Management is Essential

Before diving into the strategy and script, it’s important to understand why managing backups is crucial:

  1. Data Protection: Regular backups protect against data loss due to system failures, corruption, or accidental deletions.
  2. Business Continuity: Ensuring that you can restore critical data quickly minimizes downtime and disruption.
  3. System Integrity: Keeping historical snapshots allows you to revert to previous stable states if needed.
  4. Security: Backups provide a recovery point in case of security breaches or ransomware attacks.
  5. Compliance: Many regulations require maintaining certain data for specified periods.

Purge Strategy for Timeshift Snapshots

To maintain a balance between having sufficient recovery points and efficient storage usage, we propose the following purge strategy:

  1. Keep All Snapshots from the Last Week: Retain all snapshots from the past 7 days.
  2. Keep Weekly Snapshots for the Last Month: Retain one snapshot per week for the past month.
  3. Keep Monthly Snapshots for the Last Year: Retain one snapshot per month for the past year.
  4. Delete Older Snapshots: Remove any snapshots older than one year.

The Automated Purge Script

Here is a bash script designed to automate the purge strategy. The script stops the libvirtd service, finds and sorts all VM disk images and configuration files, then copies and purges them based on the strategy. Enhanced with console outputs, it provides clear progress and actions during execution.

#!/bin/bash

# Define the Timeshift snapshot directory
TIMESHIFT_DIR="/timeshift/snapshots"

# Get the current date
CURRENT_DATE=$(date +%s)

# Define time intervals in seconds
ONE_WEEK=$((7 * 24 * 60 * 60))
ONE_MONTH=$((30 * 24 * 60 * 60))
ONE_YEAR=$((365 * 24 * 60 * 60))

# Function to parse the date of a snapshot
get_snapshot_date() {
    SNAPSHOT_DIR=$1
    SNAPSHOT_DATE=$(basename "$SNAPSHOT_DIR" | cut -d'_' -f1)
    date -d "$SNAPSHOT_DATE" +%s
}

# Print starting message
echo "Starting Timeshift snapshot purge process..."
echo "Current date: $(date)"
echo "Timeshift snapshot directory: $TIMESHIFT_DIR"
echo ""

# Find all snapshots and sort them by date
SNAPSHOTS=$(ls -d ${TIMESHIFT_DIR}/* | sort)

# Initialize variables to track the last kept snapshots
last_weekly_snapshot=""
last_monthly_snapshot=""

# Iterate over each snapshot
for SNAPSHOT in $SNAPSHOTS; do
    SNAPSHOT_DATE=$(get_snapshot_date "$SNAPSHOT")
    AGE=$((CURRENT_DATE - SNAPSHOT_DATE))
    SNAPSHOT_DATE_HUMAN=$(date -d @"$SNAPSHOT_DATE" "+%Y-%m-%d %H:%M:%S")

    if [ $AGE -le $ONE_WEEK ]; then
        # Keep all snapshots from the last week
        echo "Keeping recent snapshot: $SNAPSHOT (Date: $SNAPSHOT_DATE_HUMAN)"
    elif [ $AGE -le $ONE_MONTH ]; then
        # Keep one snapshot per week for the past month
        WEEK_NUM=$(date -d @"$SNAPSHOT_DATE" +%U)
        if [ "$WEEK_NUM" != "$last_weekly_snapshot" ]; then
            echo "Keeping weekly snapshot: $SNAPSHOT (Date: $SNAPSHOT_DATE_HUMAN)"
            last_weekly_snapshot="$WEEK_NUM"
        else
            echo "Deleting snapshot: $SNAPSHOT (Date: $SNAPSHOT_DATE_HUMAN)"
            rm -rf "$SNAPSHOT"
        fi
    elif [ $AGE -le $ONE_YEAR ]; then
        # Keep one snapshot per month for the past year
        MONTH_NUM=$(date -d @"$SNAPSHOT_DATE" +%m)
        if [ "$MONTH_NUM" != "$last_monthly_snapshot" ]; then
            echo "Keeping monthly snapshot: $SNAPSHOT (Date: $SNAPSHOT_DATE_HUMAN)"
            last_monthly_snapshot="$MONTH_NUM"
        else
            echo "Deleting snapshot: $SNAPSHOT (Date: $SNAPSHOT_DATE_HUMAN)"
            rm -rf "$SNAPSHOT"
        fi
    else
        # Delete snapshots older than one year
        echo "Deleting old snapshot: $SNAPSHOT (Date: $SNAPSHOT_DATE_HUMAN)"
        rm -rf "$SNAPSHOT"
    fi
done

echo ""
echo "Purge completed."
echo "Remaining disk space:"
df -h "$TIMESHIFT_DIR"

Steps to Implement the Script

  1. Save the Script: Save the script above to a file, for example, timeshift_purge.sh.
  2. Make the Script Executable: Change the permissions to make the script executable.
   chmod +x timeshift_purge.sh
  1. Run the Script: Execute the script with superuser privileges.
   sudo ./timeshift_purge.sh

Automating with Cron

To ensure that the script runs regularly, you can set up a Cron job.

  1. Open Crontab:
   sudo crontab -e
  1. Add Cron Job Entry:
   0 2 * * * /usr/bin/timeshift --create --scripted && /path/to/timeshift_purge.sh

This entry schedules Timeshift to create backups and run the purge script every day at 2 AM.

Summary

By adopting a structured purge strategy and utilizing the provided bash script, you can efficiently manage your Timeshift snapshots on Arch Linux. This ensures that you have essential recovery points while optimizing storage usage. Regular backups and automated purging are key to maintaining a reliable and efficient backup system.

Implementing this solution helps safeguard your data, streamline backup management, and ensure that your system remains robust and responsive. Start implementing this strategy today to maintain the integrity and availability of your critical data.

Managing Timeshift Snapshots on Arch Linux: A Comprehensive Purge Strategy

Johannes Rest


.NET Architekt und Entwickler


Beitragsnavigation


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert