It is a best practice to backup a configuration file before performing any activity on a Linux system, which helps you to compare the configuration files in case of any issue occur.
You can automate this bash script by creating a cronjob, which can be scheduled based on your needs. However, we recommending to enable a cronjob once in a week nor month.
This script is very important for an environment where server restarts are not done frequently.
Also, as everything is a file in Linux, so it recommends running this script before fixing any VA/CR’s.
What does this script?
This script backs up the output of multiple commands and configuration files into a single file, and finally moves it to another server (aka jump server nor central server).
System details are as follows:
- Server-A: Central Server / JUMP Server (local.2daygeek.com)
- Server-B: Remote System-1 (CentOS7.2daygeek.com)
- Server-C: Remote System-2 (CentOS8.2daygeek.com)
Bash Script to Backup Configuration files on Remote Linux Server
It consists of two scripts, one is the actual script and the other is a helper script, which helps execute the actual script from the JUMP/Central server, and it pulls the output file from the remote server once the script execution is complete.
Make a Note: Both scripts must be on the central server and you must have enabled passwordless authentication for client systems for seamless operation.
Actual Script:
# vi /home/daygeek/shell-script/configuration_files_backup.sh #!/bin/bash echo -e "Hold on...Server Configuration Check is running on..." echo "========================================================" touch /tmp/$(hostname)-configuration_output-$(date +%d%b%Y).txt OUTPUT_FILE=$(ls /tmp/$(hostname)-configuration_output-$(date +%d%b%Y).txt) if [ -f $OUTPUT_FILE ] then cat /dev/null > $OUTPUT_FILE echo -e "==================================General Commands OutPut==================================" >> $OUTPUT_FILE echo "HOSTNAME-------: `hostname`" >> $OUTPUT_FILE echo "IP-------------: `hostname -i`" >> $OUTPUT_FILE echo "DATE-----------: `date`" >> $OUTPUT_FILE echo "KERNEL---------: `uname -r`" >> $OUTPUT_FILE echo -e "----------------------------/etc/hosts File OutPut----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# cat /etc/hosts" >> $OUTPUT_FILE cat /etc/hosts >> $OUTPUT_FILE echo -e "\n==================================Disk Commands/Config File OutPut==================================" >> $OUTPUT_FILE echo -e "\n----------------------------df -hT Command OutPut----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# df -hT" >> $OUTPUT_FILE df -hT >> $OUTPUT_FILE echo -e "\n----------------------------blkid Command OutPut-----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# blkid" >> $OUTPUT_FILE blkid >> $OUTPUT_FILE echo -e "\n----------------------------lsblk Command OutPut-----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# lsblk" >> $OUTPUT_FILE lsblk >> $OUTPUT_FILE echo -e "\n----------------------------PVS Command OutPut-------------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# pvs" >> $OUTPUT_FILE pvs >> $OUTPUT_FILE echo -e "\n----------------------------VGS Command OutPut-------------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# vgs" >> $OUTPUT_FILE vgs >> $OUTPUT_FILE echo -e "\n----------------------------LVS Command OutPut-------------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# lvs" >> $OUTPUT_FILE lvs >> $OUTPUT_FILE echo -e "\n----------------------------PVdisplay Command OutPut-------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# pvdisplay" >> $OUTPUT_FILE pvdisplay >> $OUTPUT_FILE echo -e "\n----------------------------VGdisplay Command OutPut-------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# vgdisplay" >> $OUTPUT_FILE vgdisplay >> $OUTPUT_FILE echo -e "\n----------------------------LVdisplay Command OutPut-------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# lvdisplay" >> $OUTPUT_FILE lvdisplay >> $OUTPUT_FILE echo -e "\n----------------------------lsscsi Command OutPut----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# lsscsi --scsi --size" >> $OUTPUT_FILE lsscsi --scsi --size >> $OUTPUT_FILE echo -e "\n----------------------------mount Command OutPut-----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# mount" >> $OUTPUT_FILE mount >> $OUTPUT_FILE echo -e "\n----------------------------/dev/mapper Devices OutPut-----------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ls -ltr /dev/mapper" >> $OUTPUT_FILE ls -ltr /dev/mapper >> $OUTPUT_FILE echo -e "\n----------------------------/proc/partitions OutPut--------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# cat /proc/partitions" >> $OUTPUT_FILE cat /proc/partitions >> $OUTPUT_FILE echo -e "\n----------------------------/etc/fstab File OutPut---------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# cat /etc/fstab" >> $OUTPUT_FILE cat /etc/fstab >> $OUTPUT_FILE echo -e "\n----------------------------/etc/sudoers File OutPut-------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# cat /etc/sudoers" >> $OUTPUT_FILE cat /etc/sudoers >> $OUTPUT_FILE echo -e "\n==================================Network Commands/Config File OutPut==================================" >> $OUTPUT_FILE echo -e "\n----------------------------ip Command OutPut--------------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ip a" >> $OUTPUT_FILE ip a >> $OUTPUT_FILE echo -e "\n----------------------------ip Command Customized OutPut---------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ip a | grep -i inet | grep -v inte6" >> $OUTPUT_FILE ip a | grep -i inet | grep -v inte6 >> $OUTPUT_FILE echo -e "\n----------------------------ip Command Customized OutPut-1-------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ip -br a" >> $OUTPUT_FILE ip -br a >> $OUTPUT_FILE echo -e "\n----------------------------ip route Command OutPut--------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ip r" >> $OUTPUT_FILE ip r >> $OUTPUT_FILE echo -e "\n----------------------------ifconfig Command OutPut--------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ifconfig -a" >> $OUTPUT_FILE ifconfig -a >> $OUTPUT_FILE echo -e "\n----------------------------netstat -ni Command OutPut-----------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# netstat -ni" >> $OUTPUT_FILE netstat -ni >> $OUTPUT_FILE echo -e "\n----------------------------netstat -nr Command OutPut-----------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# netstat -nr" >> $OUTPUT_FILE netstat -nr >> $OUTPUT_FILE echo -e "\n----------------------------netstat Command OutPut with Ports----------------" >> $OUTPUT_FILE echo -e "[`hostname`]# netstat -tplugn | grep -i LISTEN" >> $OUTPUT_FILE netstat -tplugn | grep -i LISTEN >> $OUTPUT_FILE echo -e "\n----------------------------List Network Script Files------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ls -ltrh /etc/sysconfig/network-scripts/ifcfg-*" >> $OUTPUT_FILE ls -ltrh /etc/sysconfig/network-scripts/ifcfg-* >> $OUTPUT_FILE echo -e "\n----------------------------Print each Network Script file OutPut------------" >> $OUTPUT_FILE for inet_file in `ls /etc/sysconfig/network-scripts/ifcfg-*` do echo "----------$inet_file----------" >> $OUTPUT_FILE echo -e "[`hostname`]# $inet_file" >> $OUTPUT_FILE cat $inet_file >> $OUTPUT_FILE done echo -e "\n==================================Kernel Commands/Config File OutPut==================================" >> $OUTPUT_FILE echo -e "\n----------------------------sysctl Command OutPut----------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# sysctl -p" >> $OUTPUT_FILE sysctl -p >> $OUTPUT_FILE echo -e "\n----------------------------/etc/sysctl.conf File OutPut---------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# cat /etc/sysctl.conf" >> $OUTPUT_FILE cat /etc/sysctl.conf >> $OUTPUT_FILE ps -ef | egrep 'pmon|LISTENER' | grep -v grep > /dev/null 2>&1 if [ $? -eq 0 ] then echo -e "\n==================================Oracle DB Commands/Config File OutPut==================================" >> $OUTPUT_FILE echo -e "\n----------------------------Oracle Processes OutPut--------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ps -ef | egrep 'pmon|LISTENER' | grep -v grep" >> $OUTPUT_FILE ps -ef | egrep 'pmon|LISTENER' | grep -v grep >> $OUTPUT_FILE echo -e "\n----------------------------oracleasm Command OutPut-------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# oracleasm listdisks" >> $OUTPUT_FILE oracleasm listdisks >> $OUTPUT_FILE echo -e "\n----------------------------Oracle Device file OutPut------------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# ls -ltr /dev/oracleasm/disks" >> $OUTPUT_FILE ls -ltr /dev/oracleasm/disks >> $OUTPUT_FILE else echo -e "\nThis is not an Oracle DB server..." >> $OUTPUT_FILE fi lscpu | grep -w '^Hypervisor vendor:' > /dev/null 2>&1 if [ $? -ne 0 ] then echo -e "\n==================================Multipath Command OutPut==================================" >> $OUTPUT_FILE echo -e "\n----------------------------multipath -ll Command OutPut---------------------" >> $OUTPUT_FILE echo -e "[`hostname`]# multipath -ll" >> $OUTPUT_FILE multipath -ll >> $OUTPUT_FILE else echo -e "\nThis is a VM (Virtual Machine), so multipath configuration is not applicable..." >> $OUTPUT_FILE fi echo -e "Configuration file backup has been completed and the OutPut file will be placed at '/home/daygeek/backup/' directory..." echo "============================================Configuration file backup Script Completed====================================" >> $OUTPUT_FILE else echo "OutPut file isn't found, So exit the script" >> $OUTPUT_FILE fi
Supporting nor helper script:
# vi configuration_files_backup_PUSH.sh #!/bin/bash echo -e '\n' echo "Please ENTER a list of 'HostName or IP' to take a configuration file backup, and press 'ctrl+d' to execute the script." for server in `cat` do echo -e "\n$server\n=============>>" ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=5 -o "BatchMode yes" $server 'bash -s' < /home/daygeek/shell-script/configuration_files_backup.sh scp -pr "$server:/tmp/*-configuration_output-$(date +%d%b%Y).txt" "/home/daygeek/backup/" done
Finally run the sub-script that pushes the actual script to the given target servers. This script supports dynamic inventory, which means you just need to enter a list of IP nor Host_Name
that you want to backup the configuration and hit ctrl+d
.
# sh /home/daygeek/shell-script/configuration_files_backup_PUSH.sh Please ENTER a list of 'HostName or IP' to take a configuration file backup, and press 'ctrl+d' to execute the script. CentOS7.2daygeek.com CentOS8.2daygeek.com
Output:
You can see the output similar to the below one.
[local.2daygeek.com]# cat /home/daygeek/backup/CentOS8.2daygeek.com-configuration_output-27Dec2024.txt | head -40 ==================================General Commands OutPut================================== HOSTNAME-------:CentOS8.2daygeek.com IP-------------:192.168.10.50 DATE-----------:Fri Dec 27 09:07:15 +03 2024 KERNEL---------:4.18.0-513.24.1.el8_9.x86_64 ----------------------------/etc/hosts File OutPut---------------------------- [CentOS8.2daygeek.com]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 ==================================Disk Commands/Config File OutPut================================== ----------------------------df -hT Command OutPut---------------------------- [CentOS8.2daygeek.com]# df -hT Filesystem Type Size Used Avail Use% Mounted on devtmpfs devtmpfs 16G 0 16G 0% /dev tmpfs tmpfs 16G 88K 16G 1% /dev/shm tmpfs tmpfs 16G 964K 16G 1% /run tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 15G 181M 15G 2% / /dev/mapper/centos-usr xfs 10G 6.0M 4.1G 60% /usr /dev/mapper/centos-var xfs 10G 7.9G 2.2G 79% /var /dev/mapper/centos-home xfs 10G 8.4G 1.6G 83% /home /dev/mapper/centos-opt xfs 10G 5.6G 4.5G 56% /opt /dev/mapper/centos-tmp xfs 5G 69M 5G 2% /tmp /dev/sda2 xfs 2.0G 279M 1.8G 14% /boot /dev/sda1 vfat 1022M 5.9M 1017M 1% /boot/efi tmpfs tmpfs 3.3G 0 3.3G 0% /run/user/1010 tmpfs tmpfs 3.3G 0 3.3G 0% /run/user/0 tmpfs tmpfs 3.3G 0 3.3G 0% /run/user/1008 ----------------------------blkid Command OutPut----------------------------- [CentOS8.2daygeek.com]# blkid /dev/sda1: UUID="eab5-94c3" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="d92fa769-e00f-4fd7-b6ed-ecf7224af7fa" /dev/sdc1: UUID="d17e3c31-e2c9-4f11-809c-94a549bc43b7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="ca307aa4-0866-49b1-8184-004025789e63"
Final Thoughts
I hope this shell script very useful for backup your configuration files on remote Linux system.
If you have any questions or feedback, feel free to comment below.