NSAS Wiki

Network System Access Solutions

User Tools

Site Tools


Sidebar

Contact

linux:backup_scripts
otherbackup.sh
#!/bin/bash
 
PATH=/root/bin:/root/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MACHINENAME=$(hostname).$(domainname)
DATE=$(date +%d.%m.%y)
MONTH=$(date +%B)
BACKUPDIR=/export/backup/${MACHINENAME}/others/$DATE
BACKUPIMAGEDIR=/export/backup/${MACHINENAME}/images
IMAGES_TO_KEEP=3
MYSQL_USER=root
BLOCKDEVICES=$(sfdisk -l 2>/dev/null| egrep \^Disk | awk '{print $2}' | awk -F: '{print $1}' | grep -v 'dm-*')
ROOTDEVICE=$(mount | grep "/ " | awk '{print $1}')
ROOTPARTION=${ROOTDEVICE:5}
 
test -d $BACKUPDIR || mkdir -p $BACKUPDIR
umask 077
 
mysqlbackup(){
echo -en "Backing Mysql Databases: \\t\\t"
pidof mysql >/dev/null 2>&1
if [ $? -eq 0 ] 2>/dev/null
    then
        MYSQL_DATA_DIR=$(grep datadir /etc/mysql/my.cnf | sed -e "s/.*\=//")
        for db in $(find $MYSQL_DATA_DIR/ -type d | sed -e "s/\/var\/lib\/mysql\///"); do
            mysqldump $db -a --add-drop-table --allow-keywords  -q --user=$MYSQL_USER | bzip2 -c > $BACKUPDIR/${MACHINENAME}-$db.sql.bz2 && echo done || echo failed
        done
    else
        echo "No MySql Database found"
fi
}
 
aclbackup(){
echo -en "Backing ACL Rights\\t\\t\\t\\t"
getfacl -R / 2>/dev/null | bzip2 -c > $BACKUPDIR/${MACHINENAME}-acl.txt.bz2 2>/dev/null && echo done || echo failed
}
 
debbackup(){
echo -en "Backing Deb. Packages list\\t\\t"
dpkg --get-selections | bzip2 -c > $BACKUPDIR/${MACHINENAME}-installed_packages.txt.bz2 && echo done || echo failed
}
 
bootsectorbackup(){
ROOTDEVICE=$(echo $ROOTDEVICE | sed -e s,[0-9],,g)
NAMEOFDEVICE=${ROOTDEVICE:5}
echo -en "Backing MBR: ($ROOTDEVICE)\\t\\t\\t"
dd if=$ROOTDEVICE of=$BACKUPDIR/${MACHINENAME}-mbr-$NAMEOFDEVICE.dd bs=512 count=1 >/dev/null 2>&1 && echo done || echo failed
}
 
partionsbackup(){
for DEVICE in ${BLOCKDEVICES}
    do
        echo -en "Backing Partion Tables: ($DEVICE)\\t"
        NAMEOFDEVICE=${DEVICE:5}
        sfdisk -d $DEVICE | bzip2 -c > $BACKUPDIR/${MACHINENAME}-partiontable-$NAMEOFDEVICE.sfdisk.bz2 && echo done || echo failes
    done
}
 
clonebackup(){
echo -en "Cloneing Partion: ($ROOTPARTION)\\t\\t"
if [ $(date +%d) -eq "05" ]
    then
        BACKUPFILE=$BACKUPIMAGEDIR/${MACHINENAME}-$MONTH-$ROOTPARTION.img.gz
        /usr/bin/nice -n20 /bin/dd if=/dev/$ROOTPARTION | gzip -c | dd of="$BACKUPFILE"
        if [ $? -eq 0 ]
            then
                echo done
                RMMONTH=$(date -d"$IMAGES_TO_KEEP month ago" "+%B")
                echo -en "Removing Images prior to $RMMONTH.$(date +%Y)\\t"
                rm -fv $BACKUPIMAGEDIR/${MACHINENAME}-$RMMONTH-$ROOTPARTION.img.gz && echo done || echo failed
            else
                echo failed
            fi
    else
        echo "Next Backup: 01.$(date -d"next month" "+%m.%Y")"
    fi
}
 
echo 
mysqlbackup
#aclbackup
bootsectorbackup
debbackup
partionsbackup
clonebackup
echo
sync.sh
#!/bin/bash
#===============================================================================
#
#          FILE:  sync.sh
# 
#         USAGE:  ./sync.sh 
# 
#   DESCRIPTION:  Script to sync Host_A => Host_B 
#                 only sync failures will be written to standart output.
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Michel Pelzer (PE), pe@dsa.de
#       COMPANY:  DSA GmbH
#       VERSION:  1.0
#       CREATED:  03/08/2011 09:06:07 AM CET
#      REVISION:  ---
#===============================================================================
#set -x # Display commands and their arguments as they are executed.
#set -v # Display shell input lines as they are read.
#set -n # Read commands but do not execute them. This may be used to check a shell script for syntax errors. 
 
pidof rsync >/dev/null 2>&1 && exit 1
 
HOSTNAME=$(hostname)
DOMAINNAME=$(domainname)
FQDN=${HOSTNAME}.${DOMAINNAME}
DESTHOSTNAME="${HOSTNAME}-b"
LOCALDIRS="bin etc home lib opt root usr boot export sbin var"
REMOTEDIR="/"
RSYNCOPTIONS="-aHvx --ignore-errors --delete"
RSYNC=$(which rsync)
TEE="$(which tee) -a"
USER="root"
 
TESTHOSTNAME=$(echo ${HOSTNAME} | cut -d- -f2)
if [ ${TESTHOSTNAME} = "b" ] 2>/dev/null
    then
        echo "!!! Wont sync from coldstandby system: ${HOSTNAME} !!! Sorry"
        exit 1
fi
 
for dir in ${LOCALDIRS}
    do
        TMPFILE="/tmp/$$-${dir}"
        echo "Sync form ${HOSTNAME} => ${DESTHOSTNAME} $(date)" > ${TMPFILE}
        case ${dir} in
            export) RSYNCEXCLUDE="--include=others --exclude=backup/${FQDN}/*" ;;
            home)   RSYNCEXCLUDE="--exclude=aquota*" ;;
            etc)    RSYNCEXCLUDE="--exclude=network/interfaces --exclude=mtab --exclude=issue* --exclude=hostname --exclude=hosts" ;;
            var)    RSYNCEXCLUDE="--exclude=spool/cron/crontabs --exclude=spool/squid --exclude=munin --exclude=run/* --exclude=lib/vnstat/*" ;;
            *)      RSYNCEXCLUDE="" ;;
        esac
        ${RSYNC} ${RSYNCOPTIONS} ${RSYNCEXCLUDE} /${dir} ${USER}@${DESTHOSTNAME}:${REMOTEDIR} >${TMPFILE} 2>&1
        RSYNC_ERR=$?
        if [ ${RSYNC_ERR} -ne 0 ]; then
                echo "<${RSYNC_ERR}>"
                cat ${TMPFILE}
        fi
        rm ${TMPFILE}
    done
linux/backup_scripts.txt · Last modified: 2013/08/14 12:08 by michel.pelzer