This little script performs backups of remote hosts to a location the local host. It's designed to be run on a daily basis and on the first of the month archives the previous month. It's configured via simple configuration file.
You may download a copy of this script to some location on the server that will run the script.
Generally this file is only run by root so it's saved as /root/rhsb.sh, make sure that only root may execute it.
Edit crontab to execute this file accordingly and create a configuration file.
This file determines how rhsb will behave. It specifies a host and what to backup, parameters are seperated by a pipe. In the sample below two hosts are configured for backup. The first host has both database and selected directories in the file system archived. The second host only has the file system archived. Lines that start with a hash mark are ignored.
# host|opts|dirs # opts can be db or fs seperated by : # dirs is paths sepearted by : first_host.mydomain.com|db:fs|/etc:/root:/home second_host.mydomain.com|fs|/etc:/root:/home:/var/spool/nuntius
Some notes on this script: It archives the postgres databases into a hard coded directory on the remote server, this directory should be placed in the backup directory list.
In the example below the archives are saved into /home/edoceo/bak, this should be adjusted for the readers configuration.
Notice that /home is archived from each host, this is how the PostgreSQL backup is saved.
If the rsync is consuming too much bandwidth then adding the parameter of --bwlimit=56 or something will throttle it.
#!/bin/bash
# backup-all.sh - Fetches backup from remote servers as specified in $backup_data
# Archives go here
mirror_dir="/edoceo/mirror/"
# Easy to put in file
backup_data=`/bin/grep -v '^#' ./backup-all.cfg`
for data in ${backup_data}; do
host=`/bin/echo $data | /bin/cut -d'|' -f1`
opts=`/bin/echo $data | /bin/cut -d'|' -f2`
dirs=`/bin/echo $data | /bin/cut -d'|' -f3`
# echo -e "Host: $host\n Options: $opts\n Dirs: $dirs\n"
# If run on the first backup the host directory to tgz
D=`/bin/date +%d`
if [ "$D" = "01" ]; then
X=`/bin/date +%Y-%m-%B`
/bin/tar -cz -f ${mirror_dir}${host}-$X.tgz ${mirror_dir}${host}/* >/dev/null
fi
# If Database Selected
/bin/echo "${opts}" | /bin/grep 'db' >/dev/null
if [ "$?" = "0" ]; then
# Backup the PostgreSQL databases
DB_LIST=`ssh -2C ${host} "/usr/bin/psql -l -t -U postgres |/bin/cut -d'|' -f1 |/bin/sed -e 's/ //g'"`
for DB in $DB_LIST
do
if [ "$DB" != "template0" ] && [ "$DB" != "template1" ]; then
/usr/bin/ssh -2C ${host} "/usr/bin/pg_dump -b -f /home/edoceo/bak/$DB.pgd -Fc -U postgres $DB 2>/dev/null"
fi
done
fi
# Backup File Systems
/bin/echo "${opts}" | /bin/grep 'fs' >/dev/null
if [ "$?" = "0" ]; then
# Sync the File Systems
dirs=`echo $dirs | sed 's/:/\n/g'`
for dir in ${dirs} ; do
dst="${mirror_dir}${host}"
if [ ! -d $dst ]; then
/bin/mkdir -p $dst
fi
# Can say --bwlimit=56 or 128 to throttle
/usr/bin/rsync -az -e ssh ${host}:$dir $dst
done
fi
done