diff --git a/backup-everything.sh b/backup-everything.sh index 7369c5b..89d0f8b 100755 --- a/backup-everything.sh +++ b/backup-everything.sh @@ -1,194 +1,196 @@ #!/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)" + 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" + . /root/scripts/backup/backup-instructions.conf log "backup successfully concluded"