diff --git a/backup-everything.sh b/backup-everything.sh index 937e904..dec0b78 100755 --- a/backup-everything.sh +++ b/backup-everything.sh @@ -1,235 +1,260 @@ #!/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 instructions expected in $INSTRUCTIONS" exit 1 fi # no instructions no party if [ ! -f $CONFIG ]; then echo "missing options expected in $CONFIG" exit 1 fi # default mysql commands MYSQL="mysql" MYSQLDUMP="mysqldump" # default rsync command RSYNC="rsync --archive --delete" # default base backup directory BASE=/home/backups/ravotti93 # 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 options . "$CONFIG" # full pathnames to the backup directories DAILY="$BASE"/daily DAILY_FILES="$DAILY"/files DAILY_DATABASES="$DAILY"/databases # apply the porcelain to the rsync command if [ "$PORCELAIN" = 1 ]; then RSYNC="$RSYNC --dry-run" fi 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 dbprefix string Database prefix # backup_phabricator() { local path="$1" local dbprefix="$2" 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 "$dbprefix" " " log " revert Phabricator read only mode" $configset set cluster.read-only false > /dev/null } -log "init backup in $DAILY" +# +# Backup a pathname +# +# @param path string Pathname to be backupped +# @param identifier string Optional identifier of this pathname +# +backup_path() { + local path="$1" + local identifier="$2" + + # create a default identifier + if [ -z "$identifier" ]; then + identifier=$(basename "$path") + fi -log "backup /etc" -$RSYNC /etc "$DAILY_FILES" + # check if the path exists + if [ -e "$path" ]; then + + # backup this + # force to end with a slash in order to copy the files inside the directory + $RSYNC "$path/" "$DAILY_FILES/$identifier" + else + + # no path no party + warn " path not found: $path" + fi +} + +log "init backup in $DAILY" . "$INSTRUCTIONS" log "backup successfully concluded" diff --git a/backup-instructions-example.conf b/backup-instructions-example.conf index 43143ee..0d6f034 100644 --- a/backup-instructions-example.conf +++ b/backup-instructions-example.conf @@ -1,64 +1,30 @@ ################################################################ # 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 the server configuration ## -backup_service_and_database tomcat IDOM +backup_path /etc ## -# Industrial Demo +# Just dump the 'sportabile' database ## -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 +backup_database sportabile ## -# Sportabile (can be backupped without stopping its service - just locking tables is fine) +# Stop the 'tomcat' service before dumping the IDOM database ## -backup_database sportabile - -### -# Zabbix (can be backupped without stopping its service - just locking tables is fine) -### -backup_database zabbix +backup_service_and_database tomcat IDOM ### -# Phabricator (has a read-only mode useful in this situation - no need to stop it) +# Backup a Phabricator installation and all its databases +# Note: it will be put in read-only-mode ### 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