Tag Archives: backup

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

Simple Backup / Restore DataBase in postgresql

TO make a database backup in postgreslq server you use pg_dump and psql commands.

First, locate the database:

# psql -l --username postgres --password

Then:

# pg_dump --username postgres name_db --file /dir/dir/backup.sql

To restore the database:

First create database with its respective name and roles:

# psql postgres
# postgres=# CREATE DATABASE name_db ENCODING 'UTF-8';
# postgres=# CREATE ROLE root LOGIN;

Restoring:

# psql name_db < /dir/dir/backup.sql

That’s all.