evobackup/zzz_evobackup

324 lines
12 KiB
Plaintext
Raw Normal View History

2010-09-09 01:05:15 +02:00
#!/bin/sh
#
# Script Evobackup client
# See https://gitea.evolix.org/evolix/evobackup
2019-03-31 21:30:07 +02:00
#
# Author: Gregory Colpart <reg@evolix.fr>
# Contributors:
# Romain Dessort <rdessort@evolix.fr>
# Benoît Série <bserie@evolix.fr>
# Tristan Pilat <tpilat@evolix.fr>
# Victor Laborie <vlaborie@evolix.fr>
# Jérémy Lecour <jlecour@evolix.fr>
#
# Licence: AGPLv3
#
2019-03-31 21:53:07 +02:00
# /!\ DON'T FORGET TO SET "MAIL" and "SERVERS" VARIABLES
##### Configuration ############################################################
# email adress for notifications
MAIL=jdoe@example.com
# 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=10
## We use /home/backup : feel free to use your own dir
LOCAL_BACKUP_DIR="/home/backup"
# 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 ${LOCAL_BACKUP_DIR}
2010-09-09 01:05:15 +02:00
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
## lang = C for english outputs
2017-07-18 16:02:43 +02:00
export LANGUAGE=C
export LANG=C
2015-02-03 01:06:09 +01:00
2015-11-24 19:48:56 +01:00
## Force umask
umask 077
test_server() {
item=$1
2019-03-31 21:30:07 +02:00
host=$(echo "${item}" | cut -d':' -f1)
port=$(echo "${item}" | cut -d':' -f2)
# Test if the server is accepting connections
2019-03-31 21:30:07 +02:00
ssh -q -o "ConnectTimeout ${SSH_CONNECT_TIMEOUT}" "${host}" -p "${port}" -t "exit"
# shellcheck disable=SC2181
if [ $? = 0 ]; then
return 0
else
echo "Failed to connect to \`${item}' within ${SSH_CONNECT_TIMEOUT} seconds" >&2
return 1
fi
}
pick_server() {
inc=${1:-0}
list_length=$(echo "${SERVERS}" | wc -w)
if [ "${inc}" -ge "${list_length}" ]; then
echo "No more server available" >&2
return 1
fi
salt=$(hostname | cksum | cut -d' ' -f1)
2019-03-31 21:30:07 +02:00
item=$(( ($(date +%d) + salt + inc) % list_length ))
field=$(( item + 1 ))
echo "${SERVERS}" | cut -d' ' -f${field}
}
2019-03-31 21:53:07 +02:00
## Verify other evobackup process and kill if needed
2019-03-31 21:58:01 +02:00
PIDFILE="/var/run/evobackup.pid"
if [ -e "${PIDFILE}" ]; then
pid=$(cat "${PIDFILE}")
2019-03-31 21:53:07 +02:00
# Killing the childs of evobackup.
2019-03-31 21:58:01 +02:00
for ppid in $(ps h --ppid "${pid}" -o pid | tr -s '\n' ' '); do
kill -9 "${ppid}";
2019-03-31 21:53:07 +02:00
done
# Then kill the main PID.
2019-03-31 21:58:01 +02:00
kill -9 "${pid}"
2019-03-31 21:57:46 +02:00
echo "$0 is still running (PID ${pid}). Process has been killed" >&2
2019-03-31 21:53:07 +02:00
fi
2019-03-31 21:58:01 +02:00
echo "$$" > ${PIDFILE}
2019-03-31 21:53:07 +02:00
# shellcheck disable=SC2064
2019-03-31 21:58:01 +02:00
trap "rm -f ${PIDFILE}" EXIT
2010-09-09 01:05:15 +02:00
2019-03-31 21:53:07 +02:00
##### LOCAL BACKUP #############################################################
2010-09-09 01:05:15 +02:00
2019-03-31 21:53:07 +02:00
# You can comment or uncomment sections below to customize the backup
2010-09-09 01:05:15 +02:00
## OpenLDAP : example with slapcat
2019-03-31 21:26:52 +02:00
# slapcat -l ${LOCAL_BACKUP_DIR}/ldap.bak
2010-09-09 01:05:15 +02:00
### MySQL
2010-09-09 01:05:15 +02:00
## example with global and compressed mysqldump
# mysqldump --defaults-extra-file=/etc/mysql/debian.cnf -P 3306 \
2019-03-31 21:26:52 +02:00
# --opt --all-databases --force --events --hex-blob | gzip --best > ${LOCAL_BACKUP_DIR}/mysql.bak.gz
## example with two dumps for each table (.sql/.txt) for all databases
# for i in $(echo SHOW DATABASES | mysql --defaults-extra-file=/etc/mysql/debian.cnf -P 3306 \
# | egrep -v "^(Database|information_schema|performance_schema|sys)" ); \
2018-08-20 23:00:45 +02:00
# do mkdir -p -m 700 /home/mysqldump/$i ; chown -RL mysql /home/mysqldump ; \
# mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --force -P 3306 -Q --opt --events --hex-blob --skip-comments \
# --fields-enclosed-by='\"' --fields-terminated-by=',' -T /home/mysqldump/$i $i; done
2010-09-09 01:05:15 +02:00
## example with compressed SQL dump for each databases
2018-08-20 23:00:45 +02:00
# mkdir -p -m 700 /home/mysqldump/
# for i in $(mysql --defaults-extra-file=/etc/mysql/debian.cnf -P 3306 -e 'show databases' -s --skip-column-names \
# | egrep -v "^(Database|information_schema|performance_schema|sys)"); do
# mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --force -P 3306 --events --hex-blob $i | gzip --best > /home/mysqldump/${i}.sql.gz
# done
## example with *one* uncompressed SQL dump for *one* database (MYBASE)
# mkdir -p -m 700 /home/mysqldump/MYBASE
2014-06-17 19:17:31 +02:00
# chown -RL mysql /home/mysqldump/
# mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --force -Q \
# --opt --events --hex-blob --skip-comments -T /home/mysqldump/MYBASE MYBASE
2010-09-09 01:05:15 +02:00
## example with mysqlhotcopy
2018-08-20 23:00:45 +02:00
# mkdir -p -m 700 /home/mysqlhotcopy/
2010-09-09 01:05:15 +02:00
# mysqlhotcopy BASE /home/mysqlhotcopy/
## example for multiples MySQL instances
# mysqladminpasswd=$(grep -m1 'password = .*' /root/.my.cnf|cut -d" " -f3)
# grep -E "^port\s*=\s*\d*" /etc/mysql/my.cnf |while read instance; do
# instance=$(echo "$instance"|awk '{ print $3 }')
# if [ "$instance" != "3306" ]
# then
2019-03-31 21:26:52 +02:00
# mysqldump -P $instance --opt --all-databases --hex-blob -u mysqladmin -p$mysqladminpasswd > ${LOCAL_BACKUP_DIR}/mysql.$instance.bak
# fi
# done
### PostgreSQL
## example with pg_dumpall (warning: you need space in ~postgres)
2010-09-09 01:05:15 +02:00
# su - postgres -c "pg_dumpall > ~/pg.dump.bak"
2019-03-31 21:26:52 +02:00
# mv ~postgres/pg.dump.bak ${LOCAL_BACKUP_DIR}/
## another method with gzip directly piped
# cd /var/lib/postgresql
2019-03-31 21:26:52 +02:00
# sudo -u postgres pg_dumpall | gzip > ${LOCAL_BACKUP_DIR}/pg.dump.bak.gz
# cd - > /dev/null
2010-09-09 01:05:15 +02:00
## example with all tables from MYBASE excepts TABLE1 and TABLE2
2019-03-31 21:26:52 +02:00
# pg_dump -p 5432 -h 127.0.0.1 -U USER --clean -F t --inserts -f ${LOCAL_BACKUP_DIR}/pg-backup.tar -t 'TABLE1' -t 'TABLE2' MYBASE
## example with only TABLE1 and TABLE2 from MYBASE
2019-03-31 21:26:52 +02:00
# pg_dump -p 5432 -h 127.0.0.1 -U USER --clean -F t --inserts -f ${LOCAL_BACKUP_DIR}/pg-backup.tar -T 'TABLE1' -T 'TABLE2' MYBASE
2010-09-09 01:05:15 +02:00
## MongoDB : example with mongodump
## don't forget to create use with read-only access
## > use admin
2016-08-18 18:01:07 +02:00
## > db.createUser( { user: "mongobackup", pwd: "PASS", roles: [ "backup", ] } )
2019-03-31 21:26:52 +02:00
# test -d ${LOCAL_BACKUP_DIR}/mongodump/ && rm -rf ${LOCAL_BACKUP_DIR}/mongodump/
# mkdir -p -m 700 ${LOCAL_BACKUP_DIR}/mongodump/
# mongodump --quiet -u mongobackup -pPASS -o ${LOCAL_BACKUP_DIR}/mongodump/
2017-02-07 15:26:22 +01:00
# if [ $? -ne 0 ]; then
# echo "Error with mongodump!"
# fi
2011-08-17 11:02:39 +02:00
## Redis : example with copy .rdb file
2019-03-31 21:26:52 +02:00
# cp /var/lib/redis/dump.rdb ${LOCAL_BACKUP_DIR}/
2012-10-27 18:27:18 +02:00
2018-04-05 16:24:18 +02:00
## ElasticSearch, take a snapshot as a backup.
## Warning: You need to have a path.repo configured.
## See: https://wiki.evolix.org/HowtoElasticsearch#snapshots-et-sauvegardes
# curl -s -XDELETE "localhost:9200/_snapshot/snaprepo/snapshot.daily" -o /tmp/es_delete_snapshot.daily.log
# curl -s -XPUT "localhost:9200/_snapshot/snaprepo/snapshot.daily?wait_for_completion=true" -o /tmp/es_snapshot.daily.log
## Clustered version here
## It basically the same thing except that you need to check that NFS is mounted
# if ss | grep ':nfs' | grep -q 'ip\.add\.res\.s1' && ss | grep ':nfs' | grep -q 'ip\.add\.res\.s2'
# then
# curl -s -XDELETE "localhost:9200/_snapshot/snaprepo/snapshot.daily" -o /tmp/es_delete_snapshot.daily.log
# curl -s -XPUT "localhost:9200/_snapshot/snaprepo/snapshot.daily?wait_for_completion=true" -o /tmp/es_snapshot.daily.log
# else
# echo 'Cannot make a snapshot of elasticsearch, at least one node is not mounting the repository.'
# fi
## If you need to keep older snapshot, for example the last 10 daily snapshots, replace the XDELETE and XPUT lines by :
# for snapshot in $(curl -s -XGET "localhost:9200/_snapshot/snaprepo/_all?pretty=true" | grep -Eo 'snapshot_[0-9]{4}-[0-9]{2}-[0-9]{2}' | head -n -10); do
# curl -s -XDELETE "localhost:9200/_snapshot/snaprepo/${snapshot}" | grep -v -Fx '{"acknowledged":true}'
# done
# date=$(date +%F)
# curl -s -XPUT "localhost:9200/_snapshot/snaprepo/snapshot_${date}?wait_for_completion=true" -o /tmp/es_snapshot_${date}.log
2013-11-15 12:16:50 +01:00
2016-11-09 12:10:10 +01:00
## RabbitMQ : export config
2019-03-31 21:26:52 +02:00
#rabbitmqadmin export ${LOCAL_BACKUP_DIR}/rabbitmq.config >> /var/log/evobackup.log
2016-11-09 12:10:10 +01:00
## Dump MBR / table partitions with dd and sfdisk
## Linux
#for disk in $(ls /dev/[sv]d[a-z] 2>/dev/null); do
2019-03-31 21:32:09 +02:00
# name=$(basename $disk)
# dd if=$disk of=${LOCAL_BACKUP_DIR}/MBR-$name bs=512 count=1 2>&1 | egrep -v "(records in|records out|512 bytes)"
# fdisk -l $disk > ${LOCAL_BACKUP_DIR}/partitions-$name
#done
2019-03-31 21:26:52 +02:00
#cat ${LOCAL_BACKUP_DIR}/partitions-* > ${LOCAL_BACKUP_DIR}/partitions
## OpenBSD
2019-03-31 21:26:52 +02:00
# disklabel sd0 > ${LOCAL_BACKUP_DIR}/partitions
2010-09-09 01:05:15 +02:00
# backup MegaCli config
2019-03-31 21:26:52 +02:00
#megacli -CfgSave -f ${LOCAL_BACKUP_DIR}/megacli_conf.dump -a0 >/dev/null
## Dump system and kernel versions
2019-03-31 21:26:52 +02:00
uname -a > ${LOCAL_BACKUP_DIR}/uname
## Dump network routes with mtr and traceroute (warning: could be long with aggressive firewalls)
2016-05-10 00:34:27 +02:00
for addr in 8.8.8.8 www.evolix.fr travaux.evolix.net; do
2019-03-31 21:26:52 +02:00
mtr -r ${addr} > ${LOCAL_BACKUP_DIR}/mtr-${addr}
traceroute -n ${addr} > ${LOCAL_BACKUP_DIR}/traceroute-${addr} 2>&1
2013-09-03 15:01:55 +02:00
done
2010-09-09 01:05:15 +02:00
## Dump process with ps
2019-03-31 21:26:52 +02:00
ps auwwx >${LOCAL_BACKUP_DIR}/ps.out
2010-09-09 01:05:15 +02:00
2019-03-31 21:27:10 +02:00
if [ "${SYSTEM}" = "linux" ]; then
2015-11-24 19:52:05 +01:00
## Dump network connections with netstat
2019-03-31 21:26:52 +02:00
netstat -taupen > ${LOCAL_BACKUP_DIR}/netstat.out
2013-06-30 13:18:54 +02:00
2015-11-24 19:52:05 +01:00
## List Debian packages
2019-03-31 21:26:52 +02:00
dpkg -l > ${LOCAL_BACKUP_DIR}/packages
dpkg --get-selections > ${LOCAL_BACKUP_DIR}/packages.getselections
apt-cache dumpavail > ${LOCAL_BACKUP_DIR}/packages.available
else
2015-11-24 19:52:05 +01:00
## Dump network connections with netstat
2019-03-31 21:26:52 +02:00
netstat -finet -atn > ${LOCAL_BACKUP_DIR}/netstat.out
2012-03-14 17:52:41 +01:00
2015-11-24 19:52:05 +01:00
## List OpenBSD packages
2019-03-31 21:26:52 +02:00
pkg_info -m > ${LOCAL_BACKUP_DIR}/packages
fi
2019-03-31 21:53:07 +02:00
##### 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)
2010-09-09 01:05:15 +02:00
HOSTNAME=$(hostname)
2017-05-15 19:20:44 +02:00
BEGINNING=$(/bin/date +"%d-%m-%Y ; %H:%M")
2010-09-09 01:05:15 +02:00
2019-03-31 21:27:10 +02:00
if [ "${SYSTEM}" = "linux" ]; then
rep="/bin /boot /lib /opt /sbin /usr"
2010-09-09 01:05:15 +02:00
else
2017-09-07 17:05:34 +02:00
rep="/bsd /bin /sbin /usr"
2010-09-09 01:05:15 +02:00
fi
2019-03-31 21:53:07 +02:00
# /!\ 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 \
2010-09-30 20:55:14 +02:00
--exclude "lost+found" \
--exclude ".nfs.*" \
--exclude "/var/log" \
--exclude "/var/log/evobackup*" \
--exclude "/var/lib/mysql" \
--exclude "/var/lib/postgres" \
2011-07-01 17:28:43 +02:00
--exclude "/var/lib/postgresql" \
2010-09-30 20:55:14 +02:00
--exclude "/var/lib/sympa" \
--exclude "/var/lib/metche" \
--exclude "/var/run" \
--exclude "/var/lock" \
--exclude "/var/state" \
--exclude "/var/apt" \
--exclude "/var/cache" \
--exclude "/usr/src" \
--exclude "/usr/doc" \
--exclude "/usr/share/doc" \
--exclude "/usr/obj" \
2010-10-26 01:18:17 +02:00
--exclude "dev" \
2010-09-30 20:55:14 +02:00
--exclude "/var/spool/postfix" \
--exclude "/var/lib/amavis/amavisd.sock" \
--exclude "/var/lib/munin/*tmp*" \
2010-09-30 20:55:14 +02:00
--exclude "/var/lib/php5" \
--exclude "/var/spool/squid" \
2013-11-15 12:16:50 +01:00
--exclude "/var/lib/elasticsearch" \
2014-12-22 18:35:35 +01:00
--exclude "/var/lib/amavis/tmp" \
2015-02-06 11:39:27 +01:00
--exclude "/var/lib/clamav/*.tmp" \
2015-01-16 11:38:48 +01:00
--exclude "/home/mysqltmp" \
--exclude "/var/lib/php/sessions" \
2019-03-31 21:30:07 +02:00
"${rep}" \
2010-09-30 20:55:14 +02:00
/etc \
/root \
/var \
/home \
/srv \
-e "ssh -p ${SSH_PORT}" \
"root@${SSH_SERVER}:/var/backup/" \
| tail -30 >> /var/log/evobackup.log
2010-09-09 01:05:15 +02:00
2017-05-15 19:20:44 +02:00
END=$(/bin/date +"%d-%m-%Y ; %H:%M")
2010-09-09 01:05:15 +02:00
2019-03-31 21:53:07 +02:00
##### REPORTING ################################################################
2019-03-31 21:27:10 +02:00
echo "EvoBackup - ${HOSTNAME} - START ${BEGINNING}" \
2010-09-30 20:55:14 +02:00
>> /var/log/evobackup.log
2010-09-09 01:05:15 +02:00
2019-03-31 21:27:10 +02:00
echo "EvoBackup - ${HOSTNAME} - STOP ${END}" \
2010-09-30 20:55:14 +02:00
>> /var/log/evobackup.log
2010-09-09 01:05:15 +02:00
2010-09-30 20:55:14 +02:00
tail -10 /var/log/evobackup.log | \
2019-03-31 21:27:10 +02:00
mail -s "[info] EvoBackup - Client ${HOSTNAME}" \
${MAIL}