diff --git a/.gitignore b/.gitignore index 26208ac..0aa8e46 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /backup-instructions.conf +/options.conf diff --git a/backup-everything.sh b/backup-everything.sh index 1e283be..a63fe3e 100755 --- a/backup-everything.sh +++ b/backup-everything.sh @@ -1,210 +1,226 @@ #!/bin/bash ### # Stupid script to backup some stuff # # Author: Valerio B. # Date: Wed 25 Mar 2020 ## # try to proceed even in case of errors # set -e # current directory DIR="${BASH_SOURCE%/*}" if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi # path to the instructions file INSTRUCTIONS="$DIR/backup-instructions.conf" +# path to the configuration file +CONFIG="$DIR/options.conf" + +# no instructions no party if [ ! -f $INSTRUCTIONS ]; then - echo "missing file $INSTRUCTIONS" + echo "missing instructions expected in $INSTRUCTIONS" exit 1 fi +# no instructions no party +if [ ! -f $CONFIG ]; then + echo "missing options expected in $CONFIG" + exit 1 +fi + +# mysql commands with root privileges MYSQL="mysql --defaults-file=/etc/mysql_erinformatica.cnf" MYSQLDUMP="mysqldump --defaults-file=/etc/mysql_erinformatica.cnf" +# set the base backup directory BASE=/home/backups/ravotti93 -# set to 1 to avoid any disservice +# set to 1 to avoid any disservice (e.g. systemctl stop/start) NO_DISSERVICE= # set to 1 to do nothing PORCELAIN= +# include the configuration to eventually override some constants +. "$CONFIG" + +# full pathnames to the backup directories 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" 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 to make it effective)" $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" log "backup /etc" rsync --archive --delete /etc "$DAILY_FILES" . "$INSTRUCTIONS" log "backup successfully concluded" diff --git a/backup-instructions-example.conf b/backup-instructions-example.conf index 32d1b70..43143ee 100644 --- a/backup-instructions-example.conf +++ b/backup-instructions-example.conf @@ -1,59 +1,64 @@ -# put there your stuff to be backupped +################################################################ +# Put there your stuff to be backupped # # See https://sviluppo.erinformatica.it/w/services_in_ravotti93/ # +# Notes: +# Use the 'backup-instructions-example.conf' file to create an +# 'backup-instructions.conf' file. +################################################################ ## # 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 diff --git a/options-example.conf b/options-example.conf new file mode 100644 index 0000000..dfde378 --- /dev/null +++ b/options-example.conf @@ -0,0 +1,19 @@ +# +# Put there your options +# +# Notes: +# Use the 'options-example.conf' file to create an 'options.conf' file. +####################################################################### + +# change the place where you want to put backups (no trailing slash) +BASE=/home/backups/super-server + +# uncomment these if you have custom mysql commands to be root +#MYSQL="mysql --defaults-file=/etc/mysql_erinformatica.cnf" +#MYSQLDUMP="mysqldump --defaults-file=/etc/mysql_erinformatica.cnf" + +# uncomment to avoid any disservice (e.g. systemctl stop/start) +#NO_DISSERVICE=1 + +# uncomment to test things and do nothing +#PORCELAIN=1