Tag Archives: linux

Backup Linux System

#!/bin/bash

# Global Variables
INFO_FILE="info_`hostname`_`date +%Y-%m-%d`"
BACKUP_DIRECTORY="/mnt/backup/Linux"
DIRECTORY_NAME="`hostname`"

# Create backup directory
mkdir -pv $BACKUP_DIRECTORY/$DIRECTORY_NAME

# Output commands backup
echo -e "`hostname` > cat /etc/issue" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/issue >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > uname -a" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
uname -a >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > free -m" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
free -m >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > fdisk -l" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
fdisk -l >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > df -h" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
df -h >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /etc/fstab" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/fstab >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > ifconfig" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
ifconfig >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > netstat -nr" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
netstat -nr >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /etc/rc.local" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /etc/rc.local >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > netstat -tunpl" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
netstat -tunpl >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > pstree" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
pstree >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > crontab -l" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
crontab -l >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > for i in cat /etc/passwd|awk -F: '($3>999 && $3 < 10000 ) {print $1}'; do crontab -l -u $i;done" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
for i in `cat /etc/passwd|awk -F: '($3>999 && $3 < 10000 ) {print $1}'`; do crontab -l -u $i; done >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > iptables -nvL" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
iptables -nvL >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > cat /proc/cpuinfo" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
cat /proc/cpuinfo >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > dmidecode" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
dmidecode >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > lspci" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
lspci >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > lsusb" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
lsusb >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "`hostname` > ls /etc/rc*.d" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
ls /etc/rc*.d >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE
echo -e "\n" >> $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE

# Backup all directories excluding /proc /sys /dev
tar cf $BACKUP_DIRECTORY/$DIRECTORY_NAME/$INFO_FILE.tar / --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/mnt

Change relay routing and postfix parameter perl script for postfix (failover relay)

change_relay.pl

#!/usr/bin/perl

use Term::ANSIColor;

my $relay_04_gw = "10.24.0.1";
my $relay_03_gw = "10.24.25.1";
my $relay_04_gw_interface = "eth0";
my $relay_03_gw_interface = "eth1";
my @file = split( " ", qx+cat /etc/postfix/main.cf | grep ^myhostname+);
my $hostname = $file[2];
chomp $hostname;

print color("green"), "Estado: ".$hostname."\n", color("reset");
print "Escoja la opción:\n";
print "1. Cambiar a relay-03.xxx.com\n";
print "2. Cambiar a relay-04.xxx.com\n";
my $answer = <>;
chomp $answer;

if ( $answer eq "1" ){
 my $new_hostname = "relay-03.xxx.com";
 check_hostname($new_hostname);
 change_iptables("25", "tcp", "drop");
 change_routing("delete", $relay_04_gw, $relay_04_gw_interface);
 change_routing("add", $relay_03_gw, $relay_03_gw_interface);
 system("sed -i s/$hostname/$new_hostname/g /etc/postfix/main.cf");
 system("/etc/init.d/postfix restart");
 change_iptables("25", "tcp", "accept");
 print color("red"), "Cambiado a ".$new_hostname."\n", color("reset");
}

if ( $answer eq "2" ){
 my $new_hostname = "relay-04.xxx.com";
 check_hostname($new_hostname);
 change_iptables("25", "tcp", "drop");
 change_routing("delete", $relay_03_gw, $relay_03_gw_interface);
 change_routing("add", $relay_04_gw, $relay_04_gw_interface);
 system("sed -i s/$hostname/$new_hostname/g /etc/postfix/main.cf");
 system("/etc/init.d/postfix restart");
 change_iptables("25", "tcp", "accept");
 print color("red"), "Cambiado a ".$new_hostname."\n", color("reset");
}

sub change_routing() {
 my $action = $_[0];
 my $gateway = $_[1];
 my $interface = $_[2];
 if ( $action eq "add" ){
 system("ip route add default via $gateway dev $interface");
 }
 if ( $action eq "delete" ){
 system("ip route del default via $gateway dev $interface");
 }
}

sub change_iptables() {
 my $port = $_[0];
 my $protocol = $_[1];
 my $action = $_[2];
 if ( $action eq "drop" ){
 system("iptables -I INPUT -p $protocol --dport $port -j DROP");
 }
 if ( $action eq "accept"){
 system("iptables -D INPUT -p $protocol --dport $port -j DROP");
 }
}

sub check_hostname() {
 my $new_hostname = $_[0];
 if ( $new_hostname eq $hostname ){
 die "$hostname ya se encuentra asignado\n";
 }
}