diff --git a/backup-everything.sh b/backup-everything.sh index 010817d..39774c6 100755 --- a/backup-everything.sh +++ b/backup-everything.sh @@ -1,337 +1,341 @@ #!/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 config no party if [ ! -f $CONFIG ]; then echo "missing options expected in $CONFIG" exit 1 fi # no instructions no party if [ ! -f $INSTRUCTIONS ]; then echo "missing instructions expected in $INSTRUCTIONS" exit 1 fi # default mysql commands MYSQL="mysql" MYSQLDUMP="mysqldump" # default rsync command RSYNC="rsync --archive --delete" -# default base backup directory -BASE=/home/backups +# default base backup directory for all backups +BASE="/home/backups" + +# default box name +BOX="$(hostname)" # 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 +BASEBOX="$BASE/$BOX" +DAILY="$BASEBOX/daily" +DAILY_FILES="$DAILY/files" +DAILY_DATABASES="$DAILY/databases" DAILY_LASTLOG="$DAILY/last.log" # apply the porcelain to the rsync command if [ "$PORCELAIN" = 1 ]; then RSYNC="$RSYNC --dry-run" fi ### # Print something # # It also put the message in the backup directory # # @param string severity # @param string message # function printthis() { local msg="[$(date)][$1] $@" # print to standard output echo "$msg" # put in the log file if possible if [ -f "$DAILY_LASTLOG" ]; then echo "$msg" >> "$DAILY_LASTLOG" fi } ### # Print a information message # # @param msg Message # function log() { printthis INFO $@ } ### # Print an error message # # @param msg Message # function error() { printthis ERROR $@ } ### # Print a warning message # # @param msg Message # function warn() { printthis WARN $@ } ### # Create a directory with safe permissions if it does not exist # # @param string Directory pathname # write_safe_dir_if_unexisting() { local dir="$1" # try to create the directory if it does not exists if [ ! -d "$dir" ]; then warn "creating missing $dir with 750" mkdir --parents "$dir" chmod 750 "$dir" fi # it must exists now require_existing_dir "$dir" } ### # Require an existing directory or die # # @param string Directory path # require_existing_dir() { if [ ! -d "$1" ]; then error "unexisting directory $1" exit 1 fi } # reset the last log file if exists if [ -f "$DAILY_LASTLOG" ]; then cat /dev/null > "$DAILY_LASTLOG" fi # create these pathnames if they do not exist write_safe_dir_if_unexisting "$DAILY" write_safe_dir_if_unexisting "$DAILY_FILES" write_safe_dir_if_unexisting "$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 } # # 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="$path" fi log "backup $path as $identifier" # destination directory # note that the identifier may start with a slash but this is good, just don't care dest="$DAILY_FILES/$identifier" # check if the path exists if [ -e "$path" ]; then # eventually create the destination if it does not exist mkdir --parents "$dest" # backup this # force the source to end with a slash in order to copy the files inside the directory $RSYNC "$path/" "$dest" else # no path no party warn " path not found: $path" fi } log "init backup in $DAILY" . "$INSTRUCTIONS" log "backup successfully concluded"