2010-09-09 01:05:15 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2015-08-12 13:18:47 +02:00
|
|
|
# Script Evobackup client
|
|
|
|
# See https://forge.evolix.org/projects/evobackup
|
|
|
|
#
|
2017-01-17 16:50:39 +01:00
|
|
|
# Author: Gregory Colpart <reg@evolix.fr>
|
|
|
|
# Contributor: 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
|
|
|
|
#
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## 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
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## Verify other evobackup process and kill if needed
|
2011-05-24 11:24:28 +02:00
|
|
|
PIDFILE=/var/run/evobackup.pid
|
|
|
|
if [ -e $PIDFILE ]; then
|
2017-07-18 16:02:43 +02:00
|
|
|
pid=$(cat "$PIDFILE")
|
2013-08-05 17:46:18 +02:00
|
|
|
# Killing the childs of evobackup.
|
2017-07-18 16:02:43 +02:00
|
|
|
for ppid in $(ps h --ppid "$pid" -o pid | tr -s '\n' ' '); do
|
|
|
|
kill -9 "$ppid";
|
2013-08-05 17:46:18 +02:00
|
|
|
done
|
|
|
|
# Then kill the main PID.
|
2017-07-18 16:02:43 +02:00
|
|
|
kill -9 "$pid"
|
|
|
|
echo "$0 tourne encore (PID $pid). Processus killé" >&2
|
2011-05-24 11:24:28 +02:00
|
|
|
fi
|
|
|
|
echo "$$" > $PIDFILE
|
|
|
|
trap "rm -f $PIDFILE" EXIT
|
|
|
|
|
2010-09-09 01:05:15 +02:00
|
|
|
# port SSH
|
2015-08-12 13:18:47 +02:00
|
|
|
SSH_PORT=2XXX
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
# email adress for notifications
|
2010-09-09 01:05:15 +02:00
|
|
|
MAIL=jdoe@example.com
|
|
|
|
|
2017-09-07 17:05:05 +02:00
|
|
|
# choose "linux" or "bsd"
|
|
|
|
SYSTEM=$(uname | tr '[:upper:]' '[:lower:]')
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
# Variable to choose different backup server with date
|
2017-10-09 11:34:18 +02:00
|
|
|
NODE=$(($(date +%e) % 2))
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2017-01-13 20:29:36 +01:00
|
|
|
# serveur address for rsync
|
|
|
|
SRV="node$NODE.backup.example.com"
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## We use /home/backup : feel free to use your own dir
|
2010-09-09 01:05:15 +02:00
|
|
|
mkdir -p -m 700 /home/backup
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## OpenLDAP : example with slapcat
|
2012-02-27 15:12:27 +01:00
|
|
|
# slapcat -l /home/backup/ldap.bak
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
### MySQL
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## example with global and compressed mysqldump
|
|
|
|
# mysqldump --defaults-extra-file=/etc/mysql/debian.cnf -P 3306 \
|
2018-02-08 01:44:14 +01:00
|
|
|
# --opt --all-databases --force --events --hex-blob | gzip --best > /home/backup/mysql.bak.gz
|
2012-07-30 14:52:10 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## 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)" ); \
|
2014-06-17 19:17:31 +02:00
|
|
|
# do mkdir -p /home/mysqldump/$i ; chown -RL mysql /home/mysqldump ; \
|
2018-02-08 01:44:14 +01:00
|
|
|
# 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
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## example with compressed SQL dump for each databases
|
|
|
|
# mkdir -p /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)"); 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/
|
2012-11-05 11:47:14 +01:00
|
|
|
# mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --force -Q \
|
2015-08-12 13:18:47 +02:00
|
|
|
# --opt --events --hex-blob --skip-comments -T /home/mysqldump/MYBASE MYBASE
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## example with mysqlhotcopy
|
2010-09-09 01:05:15 +02:00
|
|
|
# mkdir -p /home/mysqlhotcopy/
|
|
|
|
# mysqlhotcopy BASE /home/mysqlhotcopy/
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## example for multiples MySQL instances
|
2017-09-01 12:23:13 +02:00
|
|
|
# mysqladminpasswd=$(grep -m1 'password = .*' /root/.my.cnf|cut -d" " -f3)
|
2015-08-12 13:18:47 +02:00
|
|
|
# grep -E "^port\s*=\s*\d*" /etc/mysql/my.cnf |while read instance; do
|
2017-09-01 12:23:13 +02:00
|
|
|
# instance=$(echo "$instance"|awk '{ print $3 }')
|
2015-08-12 13:18:47 +02:00
|
|
|
# if [ "$instance" != "3306" ]
|
|
|
|
# then
|
|
|
|
# mysqldump -P $instance --opt --all-databases --hex-blob -u mysqladmin -p$mysqladminpasswd > /home/backup/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"
|
|
|
|
# mv ~postgres/pg.dump.bak /home/backup/
|
2017-04-04 09:47:47 +02:00
|
|
|
## another method with gzip directly piped
|
|
|
|
# cd /var/lib/postgresql
|
|
|
|
# sudo -u postgres pg_dumpall | gzip > /home/backup/pg.dump.bak.gz
|
2017-09-07 09:57:26 +02:00
|
|
|
# cd - > /dev/null
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## example with all tables from MYBASE excepts TABLE1 and TABLE2
|
|
|
|
# pg_dump -p 5432 -h 127.0.0.1 -U USER --clean -F t --inserts -f /home/backup/pg-backup.tar -t 'TABLE1' -t 'TABLE2' MYBASE
|
|
|
|
|
|
|
|
## example with only TABLE1 and TABLE2 from MYBASE
|
|
|
|
# pg_dump -p 5432 -h 127.0.0.1 -U USER --clean -F t --inserts -f /home/backup/pg-backup.tar -T 'TABLE1' -T 'TABLE2' MYBASE
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +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", ] } )
|
2017-02-07 10:19:53 +01:00
|
|
|
# mongodump --quiet -u mongobackup -pPASS -o /home/backup/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
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## Redis : example with copy .rdb file
|
2012-10-27 18:27:18 +02:00
|
|
|
# cp /var/lib/redis/dump.rdb /home/backup/
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## ElasticSearch : example with rsync (warning: don't forget to use NFS if you have a cluster)
|
2014-01-09 17:00:51 +01:00
|
|
|
## Disable ES translog flush
|
2015-08-12 13:18:47 +02:00
|
|
|
# curl -s -XPUT 'localhost:9200/_settings' -d '{"index.translog.disable_flush": true}' >/dev/null
|
2014-01-09 17:00:51 +01:00
|
|
|
## Flushes translog
|
2015-08-12 13:18:47 +02:00
|
|
|
# curl -s 'localhost:9200/_flush' | grep -qe '"ok":true'
|
2014-01-09 17:00:51 +01:00
|
|
|
## If it succeed, do an rsync of the datadir
|
2015-08-12 13:18:47 +02:00
|
|
|
# if [ $? -eq 0 ]; then
|
|
|
|
# rsync -a /var/lib/elasticsearch /home/backup/
|
|
|
|
# else
|
|
|
|
# echo "Error when flushing ES translog indexes."
|
|
|
|
# fi
|
2014-01-09 17:00:51 +01:00
|
|
|
## In any case re-enable translog flush
|
2015-08-12 13:18:47 +02:00
|
|
|
# curl -s -XPUT 'localhost:9200/_settings' -d '{"index.translog.disable_flush": false}' > /dev/null
|
2013-11-15 12:16:50 +01:00
|
|
|
|
2016-11-09 12:10:10 +01:00
|
|
|
## RabbitMQ : export config
|
|
|
|
#rabbitmqadmin export /home/backup/rabbitmq.config >> /var/log/evobackup.log
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## Dump MBR / table partitions with dd and sfdisk
|
2015-11-12 17:54:58 +01:00
|
|
|
## Linux
|
2017-09-01 12:23:13 +02:00
|
|
|
#for disk in $(ls /dev/[sv]d[a-z] 2>/dev/null); do
|
2017-03-27 11:33:58 +02:00
|
|
|
# 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)"
|
|
|
|
# fdisk -l $disk > /home/backup/partitions-$name
|
|
|
|
#done
|
|
|
|
#cat /home/backup/partitions-* > /home/backup/partitions
|
2015-11-12 17:54:58 +01:00
|
|
|
## OpenBSD
|
|
|
|
# disklabel sd0 > /home/backup/partitions
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-11-25 17:16:46 +01:00
|
|
|
# backup MegaCli config
|
|
|
|
#megacli -CfgSave -f /home/backup/megacli_conf.dump -a0 >/dev/null
|
|
|
|
|
2017-02-17 13:00:47 +01:00
|
|
|
## Dump system and kernel versions
|
|
|
|
uname -a > /home/backup/uname
|
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## 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
|
2013-09-03 15:01:55 +02:00
|
|
|
mtr -r $addr > /home/backup/mtr-${addr}
|
|
|
|
traceroute -n $addr > /home/backup/traceroute-${addr}
|
|
|
|
done
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2015-08-12 13:18:47 +02:00
|
|
|
## Dump process with ps
|
2011-12-30 14:13:51 +01:00
|
|
|
ps aux >/home/backup/ps.out
|
2010-09-09 01:05:15 +02:00
|
|
|
|
2017-07-18 16:02:43 +02:00
|
|
|
if [ "$SYSTEM" = "linux" ]; then
|
2015-11-24 19:52:05 +01:00
|
|
|
## Dump network connections with netstat
|
|
|
|
netstat -taupen >/home/backup/netstat.out
|
2013-06-30 13:18:54 +02:00
|
|
|
|
2015-11-24 19:52:05 +01:00
|
|
|
## List Debian packages
|
|
|
|
dpkg -l >/home/backup/packages
|
2017-07-12 01:19:22 +02:00
|
|
|
dpkg --get-selections >/home/backup/packages.getselections
|
2015-11-12 17:54:58 +01:00
|
|
|
else
|
2015-11-24 19:52:05 +01:00
|
|
|
## Dump network connections with netstat
|
|
|
|
netstat -finet -atn >/home/backup/netstat.out
|
2012-03-14 17:52:41 +01:00
|
|
|
|
2015-11-24 19:52:05 +01:00
|
|
|
## List OpenBSD packages
|
|
|
|
pkg_info -m >/home/backup/packages
|
2015-11-12 17:54:58 +01:00
|
|
|
fi
|
2015-08-12 13:18:47 +02:00
|
|
|
|
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
|
|
|
|
2017-07-18 16:02:43 +02:00
|
|
|
if [ "$SYSTEM" = "linux" ]; then
|
2010-09-30 20:55:14 +02:00
|
|
|
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
|
|
|
|
|
2014-06-19 02:43:36 +02:00
|
|
|
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" \
|
2016-04-21 11:59:51 +02:00
|
|
|
--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" \
|
2017-09-29 09:51:17 +02:00
|
|
|
--exclude "/var/lib/php/sessions" \
|
2010-09-30 20:55:14 +02:00
|
|
|
$rep \
|
|
|
|
/etc \
|
|
|
|
/root \
|
|
|
|
/var \
|
|
|
|
/home \
|
|
|
|
/srv \
|
|
|
|
-e "ssh -p $SSH_PORT" \
|
2017-07-18 16:02:43 +02:00
|
|
|
"root@$SRV:/var/backup/" \
|
2014-06-19 02:43:36 +02:00
|
|
|
| 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
|
|
|
|
2017-05-15 19:20:44 +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
|
|
|
|
2017-05-15 19:20:44 +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 | \
|
|
|
|
mail -s "[info] EvoBackup - Client $HOSTNAME" \
|
|
|
|
$MAIL
|