OS Information and Installed Package Collection Script

From Notes_Wiki

Home > Ubuntu > Ubuntu 22.04 > OS Information and Installed Package Collection Script

This script collects operating system details and installed package information from both RPM-based (e.g., RHEL, Rocky Linux) and DPKG-based (e.g., Debian, Ubuntu) systems.

It is designed to be used with the backup_script directive in rsnapshot.conf, allowing automated inclusion of system inventory data in regular backup snapshots. This is useful for auditing, compliance, and system inventory tracking.

Pre-requisites

If rsnapshot is not installed on the system, install Rsnapshot and configure as per the below article:

Rsnapshot

Client-Side Script Setup (RPM or Debian-Based Machines)

Create the Script File

Create a file named system_info.sh with the following content:

#!/bin/bash

OUTPUT="/root/system_info.txt"

# Overwrite old content with new system info
{
    echo "===== OS Release Information ====="
    cat /etc/*release 2>/dev/null

    echo -e "\n===== Installed Packages ====="

    # Detect package manager and run the appropriate command
    if command -v rpm &>/dev/null; then
        echo -e "\n-- Using rpm:"
        rpm -qa
    fi

    if command -v dpkg &>/dev/null; then
        echo -e "\n-- Using dpkg:"
        dpkg --list
    fi
} > "$OUTPUT"

Make the Script Executable

chmod +x /root/system_info.sh

You can save this script in any preferred directory, such as /root, /opt, or /etc, based on your system organization and permissions.

Calling the Script from Rsnapshot

Add the following line to the /etc/rsnapshot.conf file to execute the script before backups:

backup_script   /usr/bin/ssh root@<client-hostname> "/root/system_info.sh"   <foldername>/.ignore1/
  • Replace client-hostname with the target client machine.
  • Replace foldername with the appropriate identifier.

To include the system_info.txt output file in the Rsnapshot backup, add the following line to /etc/rsnapshot.conf:

backup   root@<client-hostname>:/root/system_info.txt   <foldername>/

Cron Job (Optional): Run Script Weekly

If you want to run this script locally on the client machine, configure the following cron job: To run the script weekly (e.g., every Sunday at 2:00 AM), add the following line to the root user’s crontab:

0 2 * * 0 /root/system_info.sh >> /var/log/system_info.log 2>&1

This ensures that system information is regularly collected and logged, even outside of Rsnapshot runs.

Result

The script generates /root/system_info.txt, which is then included in the Rsnapshot backup.

This helps maintain a historical record of installed packages and OS information across systems.