Difference between revisions of "OS Information and Installed Package Collection Script"

From Notes_Wiki
Line 1: Line 1:
[[Main Page|Home]] > [[Ubuntu]] > [[Ubuntu 22.04]] > [[OS Information and Installed Package Collection Script]]
[[Main Page|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:
<pre>
#!/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"
</pre>
=== Make the Script Executable ===
<pre>
chmod +x /root/system_info.sh
</pre>
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:
<pre>
backup_script  /usr/bin/ssh root@<client-hostname> "/root/system_info.sh"  <foldername>/.ignore1/
</pre>
* 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''':
<pre>
backup  root@<client-hostname>:/root/system_info.txt  <foldername>/
</pre>
=== 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:
<pre>
0 2 * * 0 /root/system_info.sh >> /var/log/system_info.log 2>&1
</pre>
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.

Revision as of 12:31, 23 May 2025

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.