diff --git a/backup-everything.sh b/backup-everything.sh new file mode 100755 index 0000000..7369c5b --- /dev/null +++ b/backup-everything.sh @@ -0,0 +1,194 @@ +#!/bin/sh +### +# Stupid script to backup some stuff +# +# Author: Valerio B. +# Date: Wed 25 Mar 2020 +## + +# try to proceed even in case of errors +# set -e + +MYSQL="mysql --defaults-file=/etc/mysql_erinformatica.cnf" +MYSQLDUMP="mysqldump --defaults-file=/etc/mysql_erinformatica.cnf" + +BASE=/home/backups/ravotti93 + +# set to 1 to avoid any disservice +NO_DISSERVICE= + +# set to 1 to do nothing +PORCELAIN= + +DAILY="$BASE"/daily +DAILY_FILES="$DAILY"/files +DAILY_DATABASES="$DAILY"/databases + +function log() { + echo "[$(date)][INFO] $@" +} + +function error() { + echo "[$(date)][ERROR] $@" +} + +function warn() { + echo "[$(date)][WARN] $@" +} + +require_existing_dir() { + if [ ! -d "$1" ]; then + error "unexisting directory $1" + exit 1 + fi +} + +require_existing_dir "$DAILY" +require_existing_dir "$DAILY_FILES" +require_existing_dir "$DAILY_DATABASES" + +# +# Dump and compress a database +# +# @param db string Database name +# @param spaces string Cosmetic spaces +# +backup_database() { + local db="$1" + local spaces="$2" + local path="$DAILY_DATABASES"/"$db".sql.gz + + log "$spaces""dumping database $db" + + # no database no party + if ! $MYSQL "$db" -e exit > /dev/null 2>&1; then + warn "$spaces"" skip unexisting database" + return + fi + + if [ "$PORCELAIN" != 1 ]; then + $MYSQLDUMP "$db" | gzip > "$path" + fi +} + +# +# Dump and compress some databases +# +# @param database... string Database names +# +backup_databases() { + for database in $@; do + backup_database "$database" + done +} + +# +# Dump some databases with a shared prefix +# +# @param prefix string Database prefix +# @param spaces string Cosmetic spaces +# +backup_databases_prefixed() { + local prefix="$1" + local spaces="$2" + + log "$spaces""backup all databases prefixed with: $prefix" + + databases=$($MYSQL -e 'SHOW DATABASES' | grep "$prefix") + for database in $databases; do + backup_database "$database" "$spaces " + done +} + +# +# Backup a database that is used by a service +# +# The service will be stopped before dumping. +# +# @param service string Systemd unit file +# @param db string Database name +# +backup_service_and_database() { + local service="$1" + local db="$2" + local is_active= + + log "start backup service '$service' with database '$db'" + + # check if the service is running + if systemctl is-active "$service" > /dev/null; then + is_active=1 + fi + + # eventually stop the service + if [ $is_active == 1 ]; then + if [ "$NO_DISSERVICE" == 1 ]; then + warn " NOT stopping service: $service (to avoid any disservice)" + else + log " stopping service: $service" + exit + + if [ "$PORCELAIN" != 1 ]; then + systemctl stop "$service" + fi + fi + else + log " service already inactive: $service" + fi + + # backup the database now that the service is down + backup_database "$db" " " + + # eventually start again the service + if [ $is_active == 1 ]; then + if [ "$NO_DISSERVICE" == 1 ]; then + warn " NOT starting again service: $service (to avoid any disservice)" + else + log " starting again service: $service" + + if [ "$PORCELAIN" != 1 ]; then + systemctl start "$service" + fi + fi + fi +} + +# +# Backup some databases used by a service +# +# The service will be stopped before dumping the databases +# +# @param service string Systemd unit file +# @param db... string Database names +# +backup_service_and_databases() { + backup_service_and_database $@ +} + +# +# Backup phabricator +# +# @param path string Phabricator webroot +# @param +# +backup_phabricator() { + local path="$1" + local configset="$path"/bin/config + + log "backup Phabricator databases" + + log " set Phabricator read only mode (and wait some time)" + $configset set cluster.read-only true > /dev/null + sleep 5 + + backup_databases_prefixed phabricator_ " " + + log " revert Phabricator read only mode" + $configset set cluster.read-only false > /dev/null +} + +log "init backup in $DAILY" + +. /root/scripts/backup/backup-instructions.conf + +log "backup successfully concluded" diff --git a/backup-instructions.conf b/backup-instructions.conf new file mode 100644 index 0000000..32d1b70 --- /dev/null +++ b/backup-instructions.conf @@ -0,0 +1,59 @@ +# put there your stuff to be backupped +# +# See https://sviluppo.erinformatica.it/w/services_in_ravotti93/ +# + +## +# Transport Demo +## +backup_service_and_database tomcat IDOM + +## +# Industrial Demo +## +backup_service_and_database tomcat_7080 IDOM_7080 + +## +# SKI Demo +## +backup_service_and_database tomcat_ski SKI_IDOM + +## +# Tosa Cloud +## +backup_service_and_database tomcat_cloud TOSA_CLOUD + +## +# Sportabile (can be backupped without stopping its service - just locking tables is fine) +## +backup_database sportabile + +### +# Zabbix (can be backupped without stopping its service - just locking tables is fine) +### +backup_database zabbix + +### +# Phabricator (has a read-only mode useful in this situation - no need to stop it) +### +backup_phabricator /var/www/phabricator/phabricator phabricator_ + +## +# LEGACY PROJECT TO BE DELETED (maintainer Alessandro) +## +backup_database IDOM_8280 + +## +# Face Badge +## +backup_database FACE_BADGE + +## +# EWM +## +backup_databases EWM EWM_Q + +## +# Digital Invoice +## +backup_databases FE FE_DEMO