Merge branch 'multi-servers-fallback' of evolix/evobackup into master
This commit is contained in:
commit
933b0c6842
129
zzz_evobackup
129
zzz_evobackup
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# Script Evobackup client
|
# Script Evobackup client
|
||||||
# See https://gitea.evolix.org/evolix/evobackup
|
# See https://gitea.evolix.org/evolix/evobackup
|
||||||
#
|
#
|
||||||
# Author: Gregory Colpart <reg@evolix.fr>
|
# Author: Gregory Colpart <reg@evolix.fr>
|
||||||
# Contributors:
|
# Contributors:
|
||||||
# Romain Dessort <rdessort@evolix.fr>
|
# Romain Dessort <rdessort@evolix.fr>
|
||||||
|
@ -13,14 +13,26 @@
|
||||||
#
|
#
|
||||||
# Licence: AGPLv3
|
# Licence: AGPLv3
|
||||||
#
|
#
|
||||||
# The following variables must be changed:
|
# /!\ DON'T FORGET TO SET "MAIL" and "SERVERS" VARIABLES
|
||||||
# SSH_PORT: The Port used for the ssh(1) jail on the backup server
|
|
||||||
# MAIL: The email address to send notifications to.
|
##### Configuration ###################################################
|
||||||
# SRV: The hostname or IP address of the backup server.
|
|
||||||
#
|
# email adress for notifications
|
||||||
# You must then uncomment the various
|
MAIL=jdoe@example.com
|
||||||
# examples that best suit your case
|
|
||||||
#
|
# list of hosts (hostname or IP) and SSH port for Rsync
|
||||||
|
SERVERS="node0.backup.example.com:2XXX node1.backup.example.com:2XXX"
|
||||||
|
|
||||||
|
# timeout (in seconds) for the SSH test
|
||||||
|
SSH_CONNECT_TIMEOUT=30
|
||||||
|
|
||||||
|
# You can set "linux" or "bsd" manually or let it choose automatically
|
||||||
|
SYSTEM=$(uname | tr '[:upper:]' '[:lower:]')
|
||||||
|
|
||||||
|
##### SETUP AND FUNCTIONS #############################################
|
||||||
|
|
||||||
|
# shellcheck disable=SC2174
|
||||||
|
mkdir -p -m 700 /home/backup
|
||||||
|
|
||||||
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
|
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
|
||||||
|
|
||||||
|
@ -31,6 +43,50 @@ export LANG=C
|
||||||
## Force umask
|
## Force umask
|
||||||
umask 077
|
umask 077
|
||||||
|
|
||||||
|
# Call test_server with "HOST:PORT" string
|
||||||
|
# It will return with 0 if the server is reachable.
|
||||||
|
# It will return with 1 and a message on stderr if not.
|
||||||
|
test_server() {
|
||||||
|
item=$1
|
||||||
|
# split HOST and PORT from the input string
|
||||||
|
host=$(echo "${item}" | cut -d':' -f1)
|
||||||
|
port=$(echo "${item}" | cut -d':' -f2)
|
||||||
|
|
||||||
|
# Test if the server is accepting connections
|
||||||
|
ssh -q -o "ConnectTimeout ${SSH_CONNECT_TIMEOUT}" "${host}" -p "${port}" -t "exit"
|
||||||
|
# shellcheck disable=SC2181
|
||||||
|
if [ $? = 0 ]; then
|
||||||
|
# SSH connection is OK
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
# SSH connection failed
|
||||||
|
echo "Failed to connect to \`${item}' within ${SSH_CONNECT_TIMEOUT} seconds" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
# Call pick_server with an optional positive integer to get the nth server in the list.
|
||||||
|
pick_server() {
|
||||||
|
increment=${1:-0}
|
||||||
|
list_length=$(echo "${SERVERS}" | wc -w)
|
||||||
|
|
||||||
|
if [ "${increment}" -ge "${list_length}" ]; then
|
||||||
|
# We've reached the end of the list
|
||||||
|
echo "No more server available" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# A salt is useful to randomize the starting point in the list
|
||||||
|
# but stay identical each time it's called for a server (based on hostname).
|
||||||
|
salt=$(hostname | cksum | cut -d' ' -f1)
|
||||||
|
# Pick an integer between 0 and the length of the SERVERS list
|
||||||
|
# It changes each day
|
||||||
|
item=$(( ($(date +%d) + salt + increment) % list_length ))
|
||||||
|
# cut starts counting fields at 1, not 0.
|
||||||
|
field=$(( item + 1 ))
|
||||||
|
|
||||||
|
echo "${SERVERS}" | cut -d' ' -f${field}
|
||||||
|
}
|
||||||
|
|
||||||
## Verify other evobackup process and kill if needed
|
## Verify other evobackup process and kill if needed
|
||||||
PIDFILE=/var/run/evobackup.pid
|
PIDFILE=/var/run/evobackup.pid
|
||||||
if [ -e $PIDFILE ]; then
|
if [ -e $PIDFILE ]; then
|
||||||
|
@ -41,28 +97,15 @@ if [ -e $PIDFILE ]; then
|
||||||
done
|
done
|
||||||
# Then kill the main PID.
|
# Then kill the main PID.
|
||||||
kill -9 "$pid"
|
kill -9 "$pid"
|
||||||
echo "$0 tourne encore (PID $pid). Processus killé" >&2
|
echo "$0 is still running (PID $pid). Process has been killed" >&2
|
||||||
fi
|
fi
|
||||||
echo "$$" > $PIDFILE
|
echo "$$" > $PIDFILE
|
||||||
|
# shellcheck disable=SC2064
|
||||||
trap "rm -f $PIDFILE" EXIT
|
trap "rm -f $PIDFILE" EXIT
|
||||||
|
|
||||||
# port SSH
|
##### LOCAL BACKUP ####################################################
|
||||||
SSH_PORT=2XXX
|
|
||||||
|
|
||||||
# email adress for notifications
|
# You can comment or uncomment sections below to customize the backup
|
||||||
MAIL=jdoe@example.com
|
|
||||||
|
|
||||||
# choose "linux" or "bsd"
|
|
||||||
SYSTEM=$(uname | tr '[:upper:]' '[:lower:]')
|
|
||||||
|
|
||||||
# Variable to choose different backup server with date
|
|
||||||
NODE=$(($(date +%e) % 2))
|
|
||||||
|
|
||||||
# serveur address for rsync
|
|
||||||
SRV="node$NODE.backup.example.com"
|
|
||||||
|
|
||||||
## We use /home/backup : feel free to use your own dir
|
|
||||||
mkdir -p -m 700 /home/backup
|
|
||||||
|
|
||||||
## OpenLDAP : example with slapcat
|
## OpenLDAP : example with slapcat
|
||||||
# slapcat -l /home/backup/ldap.bak
|
# slapcat -l /home/backup/ldap.bak
|
||||||
|
@ -164,9 +207,9 @@ mkdir -p -m 700 /home/backup
|
||||||
## Dump MBR / table partitions with dd and sfdisk
|
## Dump MBR / table partitions with dd and sfdisk
|
||||||
## Linux
|
## Linux
|
||||||
#for disk in $(ls /dev/[sv]d[a-z] 2>/dev/null); do
|
#for disk in $(ls /dev/[sv]d[a-z] 2>/dev/null); do
|
||||||
# name=$(basename $disk)
|
# name=$(basename $disk)
|
||||||
# dd if=$disk of=/home/backup/MBR-$name bs=512 count=1 2>&1 | egrep -v "(records in|records out|512 bytes)"
|
# dd if=$disk of=/home/backup/MBR-$name bs=512 count=1 2>&1 | egrep -v "(records in|records out|512 bytes)"
|
||||||
# fdisk -l $disk > /home/backup/partitions-$name
|
# fdisk -l $disk > /home/backup/partitions-$name
|
||||||
#done
|
#done
|
||||||
#cat /home/backup/partitions-* > /home/backup/partitions
|
#cat /home/backup/partitions-* > /home/backup/partitions
|
||||||
## OpenBSD
|
## OpenBSD
|
||||||
|
@ -203,6 +246,25 @@ else
|
||||||
pkg_info -m >/home/backup/packages
|
pkg_info -m >/home/backup/packages
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
##### REMOTE BACKUP ###################################################
|
||||||
|
|
||||||
|
n=0
|
||||||
|
server=""
|
||||||
|
while :; do
|
||||||
|
server=$(pick_server "${n}")
|
||||||
|
test $? = 0 || exit 2
|
||||||
|
|
||||||
|
if test_server "${server}"; then
|
||||||
|
break
|
||||||
|
else
|
||||||
|
server=""
|
||||||
|
n=$(( n + 1 ))
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
SSH_SERVER=$(echo "${server}" | cut -d':' -f1)
|
||||||
|
SSH_PORT=$(echo "${server}" | cut -d':' -f2)
|
||||||
|
|
||||||
HOSTNAME=$(hostname)
|
HOSTNAME=$(hostname)
|
||||||
|
|
||||||
BEGINNING=$(/bin/date +"%d-%m-%Y ; %H:%M")
|
BEGINNING=$(/bin/date +"%d-%m-%Y ; %H:%M")
|
||||||
|
@ -213,6 +275,9 @@ else
|
||||||
rep="/bsd /bin /sbin /usr"
|
rep="/bsd /bin /sbin /usr"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# /!\ DO NOT USE COMMENTS in the rsync command /!\
|
||||||
|
# It breaks the command and destroys data, simply remove (or add) lines.
|
||||||
|
|
||||||
rsync -avzh --stats --delete --delete-excluded --force --ignore-errors --partial \
|
rsync -avzh --stats --delete --delete-excluded --force --ignore-errors --partial \
|
||||||
--exclude "lost+found" \
|
--exclude "lost+found" \
|
||||||
--exclude ".nfs.*" \
|
--exclude ".nfs.*" \
|
||||||
|
@ -250,12 +315,14 @@ rsync -avzh --stats --delete --delete-excluded --force --ignore-errors --partial
|
||||||
/home \
|
/home \
|
||||||
/srv \
|
/srv \
|
||||||
-e "ssh -p $SSH_PORT" \
|
-e "ssh -p $SSH_PORT" \
|
||||||
"root@$SRV:/var/backup/" \
|
"root@$SSH_SERVER:/var/backup/" \
|
||||||
| tail -30 >> /var/log/evobackup.log
|
| tail -30 >> /var/log/evobackup.log
|
||||||
|
|
||||||
END=$(/bin/date +"%d-%m-%Y ; %H:%M")
|
END=$(/bin/date +"%d-%m-%Y ; %H:%M")
|
||||||
|
|
||||||
echo "EvoBackup - $HOSTNAME - START $BEGINNING" \
|
##### REPORTING #######################################################
|
||||||
|
|
||||||
|
echo "EvoBackup - $HOSTNAME - START $BEGINNING" \
|
||||||
>> /var/log/evobackup.log
|
>> /var/log/evobackup.log
|
||||||
|
|
||||||
echo "EvoBackup - $HOSTNAME - STOP $END" \
|
echo "EvoBackup - $HOSTNAME - STOP $END" \
|
||||||
|
|
Loading…
Reference in a new issue