inital commit
This commit is contained in:
253
rsnbackup.sh
Normal file
253
rsnbackup.sh
Normal file
@ -0,0 +1,253 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# load global vars
|
||||
#
|
||||
. /etc/rsnbackup.conf
|
||||
|
||||
#
|
||||
# function to add new host for backup
|
||||
# param: $1 = BACKUP_DIR, $2 = BACKUP_HOST
|
||||
#
|
||||
ADD_NEW_HOST(){
|
||||
|
||||
#
|
||||
# create new backup directory
|
||||
#
|
||||
echo "1. Create new backup dir: $1"
|
||||
mkdir -p $1
|
||||
mkdir -p $1/.sync
|
||||
|
||||
#
|
||||
# create new rsnapshot config
|
||||
#
|
||||
echo "2. Create new rsnapshot-common.conf at $1/"
|
||||
|
||||
echo -e "
|
||||
|
||||
include_conf\t/etc/rsnapshot-common.conf
|
||||
snapshot_root\t$1/
|
||||
|
||||
#retain\thourly\t24
|
||||
retain\tdaily\t7
|
||||
retain\tweekly\t4
|
||||
retain\tmonthly\t3
|
||||
|
||||
logfile\t$1/rsnapshot.log
|
||||
lockfile\t$1/rsnapshot.pid
|
||||
|
||||
exclude_file\t$1/exclude_file.txt
|
||||
|
||||
backup\troot@$2:/\t./" >> $1/rsnapshot.conf
|
||||
|
||||
|
||||
#
|
||||
# create the "exclude" file for this host.
|
||||
#
|
||||
echo "- /sys/*
|
||||
- /proc/*
|
||||
- /tmp/*
|
||||
- /dev/*
|
||||
- /var/lock/*
|
||||
- /var/run/*
|
||||
- lost+found
|
||||
- /var/log/*
|
||||
- /var/lib/mysql/*
|
||||
- /var/lib/postgresql/*
|
||||
- /media/*
|
||||
- /restore/*
|
||||
+ /* " >> $1/exclude_file.txt
|
||||
|
||||
#
|
||||
# add this host to root's crontab file.
|
||||
#
|
||||
echo "3. Create new crontab for $2 backup."
|
||||
echo "
|
||||
# backup entrys for $2
|
||||
00 04 * * * rsnbackup -b $2 -r daily
|
||||
00 02 * * 0 rsnbackup -b $2 -r weekly
|
||||
00 00 1 * * rsnbackup -b $2 -r monthly
|
||||
" >> /var/spool/cron/crontabs/root
|
||||
|
||||
#
|
||||
# Copy the Rsnapshot root's public key to the new host we are going to
|
||||
# back up. This is so it can be trusted by the host to run backups.
|
||||
#
|
||||
cd /root
|
||||
|
||||
echo "4. Create new .ssh dir if not exist on $2."
|
||||
ssh root@$2 "test -d '.ssh' || ( mkdir .ssh; chmod 700 .ssh; touch .ssh/authorized_keys; )"
|
||||
|
||||
echo "5. Copy public_key to $2."
|
||||
cat .ssh/id_dsa.pub | ssh root@$2 "cat >> ~/.ssh/authorized_keys"
|
||||
|
||||
|
||||
#
|
||||
# set customer mail file for backup status
|
||||
#
|
||||
echo "6. Create Mail-CC file."
|
||||
echo "###
|
||||
### set customer email for notification
|
||||
###
|
||||
### Example: user1@local user2@local
|
||||
###
|
||||
CC_MAIL='' " >> $1/mail-cc.txt
|
||||
|
||||
|
||||
#
|
||||
# create NFS exports file
|
||||
#
|
||||
echo "7. Create NFS exports entry."
|
||||
if $(test -f /etc/exports) ; then
|
||||
echo "$1 $2(ro,sync,no_subtree_check)" >> "/etc/exports"
|
||||
else
|
||||
touch /etc/exports
|
||||
echo "$1 $2(ro,sync,no_subtree_check)" >> "/etc/exports"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
#
|
||||
# run rsnapshot backup param: $1 = hostname, $2 = rotation
|
||||
# [daily|weekly|monthly]
|
||||
#
|
||||
RUN_BACKUP(){
|
||||
|
||||
#
|
||||
# check advanced mailx package (heirloom-mailx) installed
|
||||
#
|
||||
if ! $(dpkg-query -Wf'${db:Status-abbrev}' heirloom-mailx 2>/dev/null | grep -q '^i') ; then
|
||||
echo "Error: Package heirloom-mailx not installed"
|
||||
exit
|
||||
fi
|
||||
|
||||
|
||||
#
|
||||
# check free hd space
|
||||
#
|
||||
GET_PERCENTAGE='s/.* \([0-9]\{1,3\}\)%.*/\1/'
|
||||
|
||||
HD_SPACE_MAX_USED=`expr 100 - $HD_SPACE_MIN_FREE`
|
||||
|
||||
if $HD_SPACE_CHECK ; then
|
||||
KB_USED=`df /$BACKUP_BASE | tail -n1 | sed -e "$GET_PERCENTAGE"`
|
||||
INODE_USED=`df -i /$BACKUP_BASE | tail -n1 | sed -e "$GET_PERCENTAGE"`
|
||||
if [ $KB_USED -ge $HD_SPACE_MAX_USED -o $INODE_USED -ge $HD_SPACE_MAX_USED ] ; then
|
||||
echo "Fatal: Not enough space left for rsnbackup backups!"
|
||||
echo "Fatal: Not enough space left for rsnapshot backups!" > /tmp/rsnapshot.mail.txt
|
||||
SEND_EMAIL "Backup Error" "/tmp/rsnapshot.mail.txt" $1
|
||||
logger "Fatal: Not enough space left for rsnbackup backups!"
|
||||
exit
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
#
|
||||
# clean old log files
|
||||
#
|
||||
if $(test -f $BACKUP_BASE/$1/rsnapshot.log) ; then
|
||||
echo "" > $BACKUP_BASE/$1/rsnapshot.log
|
||||
fi
|
||||
|
||||
|
||||
#
|
||||
# start rsnapshot backup
|
||||
#
|
||||
logger "Start rsnbackup for host: $1"
|
||||
|
||||
rsnapshot -c $BACKUP_BASE/$1/rsnapshot.conf $2
|
||||
|
||||
if [ $? = 0 ] ; then
|
||||
SEND_EMAIL "Success $2 Backup: $1" "$BACKUP_BASE/$1/rsnapshot.log" $1
|
||||
else
|
||||
SEND_EMAIL "Error $2 Backup: $1" "$BACKUP_BASE/$1/rsnapshot.log" $1
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#
|
||||
# send mail function param: $1 betreff, $2 content file $host
|
||||
#
|
||||
SEND_EMAIL(){
|
||||
|
||||
. $BACKUP_BASE/$3/mail-cc.txt
|
||||
|
||||
if [ "$CC_MAIL" == "" ] ; then
|
||||
mailx -s "$1" -S smtp=smtp://$MAIL_HOST:25 -S from="$MAIL_FROM" "$MAIL_TO" < "$2"
|
||||
else
|
||||
mailx -s "$1" -S smtp=smtp://$MAIL_HOST:25 -S from="$MAIL_FROM" -c "$CC_MAIL" "$MAIL_TO" < "$2"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
|
||||
PRINT_HELP(){ cat >&2 <<EOF
|
||||
|
||||
Usage: $0 [OPTION] Options:
|
||||
-b, --backup Backup the Host with rsnapshot
|
||||
-r, --rotate Backup rotation period
|
||||
-a, --add Add new host for backup
|
||||
-h, --help Print help and exit.
|
||||
-v, --version Print version and exit.
|
||||
|
||||
Example:
|
||||
Add new host configuration: rsnbackup -a HOSTNAME
|
||||
Run manually daily backup: rsnbackup -b HOSTNANE -r [daily|weekly|monthly]
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
PRINT_VERSION(){ cat >&2 <<EOF
|
||||
|
||||
rsnbackup version 0.0.1
|
||||
Copyright (C)2014 by Ralf Kirchner <http://www.sns-systemhaus.de/>.
|
||||
|
||||
rsnbackup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome
|
||||
to redistribute it under certain conditions. See the GNU General Public Licence for details.
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
# Execute getopt on the arguments passed to this program, identified by
|
||||
# the special character $@
|
||||
PARSED_OPTIONS=$(getopt -n "$0" -o b:r:a:hv --long "backup:,rotation;,add:,help,version" -- "$@")
|
||||
|
||||
#Bad arguments, something has gone wrong with the getopt command.
|
||||
if [ $? -ne 0 ]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# A little magic, necessary when using getopt.
|
||||
eval set -- "$PARSED_OPTIONS"
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
|
||||
-h|--help)
|
||||
PRINT_HELP
|
||||
shift;;
|
||||
|
||||
-v|--version)
|
||||
PRINT_VERSION
|
||||
shift;;
|
||||
|
||||
-a|--add)
|
||||
if [ -n "$2" ]; then
|
||||
BACKUP_DIR="$BACKUP_BASE/$2"
|
||||
BACKUP_HOST=$2
|
||||
ADD_NEW_HOST $BACKUP_DIR $BACKUP_HOST
|
||||
fi
|
||||
break;;
|
||||
|
||||
-b|--backup)
|
||||
if [ -n "$2" ]; then
|
||||
RUN_BACKUP $2 $4
|
||||
fi
|
||||
shift 4;;
|
||||
|
||||
--)
|
||||
shift
|
||||
break;;
|
||||
esac
|
||||
done
|
Reference in New Issue
Block a user