From da5dc6f4bcd56960adf490a30d48b8fc7c01089d Mon Sep 17 00:00:00 2001 From: deajan Date: Fri, 9 Sep 2016 12:31:54 +0200 Subject: [PATCH] Improved upgrade script --- upgrade-v1.x-2.1x.sh | 296 ++++++++++++++++++++++++++++--------------- 1 file changed, 194 insertions(+), 102 deletions(-) diff --git a/upgrade-v1.x-2.1x.sh b/upgrade-v1.x-2.1x.sh index 496db14..c73c7ad 100755 --- a/upgrade-v1.x-2.1x.sh +++ b/upgrade-v1.x-2.1x.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -PROGRAM="obackup config file upgrade script" +PROGRAM="obackup.upgrade" SUBPROGRAM="obackup" AUTHOR="(C) 2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr/obacup - ozy@netpower.fr" @@ -8,12 +8,181 @@ OLD_PROGRAM_VERSION="v1.x" NEW_PROGRAM_VERSION="v2.1x" PROGRAM_BUILD=2016090901 -## type -p does not work on platforms other than linux (bash). If if does not work, always as$ if ! type "$BASH" > /dev/null; then echo "Please run this script only with bash shell. Tested on bash >= 3.2" exit 127 fi +# Defines all keywords / value sets in obackup configuration files +# bash does not support two dimensional arrays, so we declare two arrays: +# ${KEYWORDS[index]}=${VALUES[index]} + +KEYWORDS=( +INSTANCE_ID +LOGFILE +SQL_BACKUP +FILE_BACKUP +BACKUP_TYPE +SQL_STORAGE +FILE_STORAGE +ENCRYPTION +CRYPT_STORAGE +GPG_RECIPIENT +CREATE_DIRS +KEEP_ABSOLUTE_PATHS +BACKUP_SIZE_MINIMUM +GET_BACKUP_SIZE +SQL_WARN_MIN_SPACE +FILE_WARN_MIN_SPACE +REMOTE_SYSTEM_URI +SSH_RSA_PRIVATE_KEY +SSH_COMPRESSION +SSH_IGNORE_KNOWN_HOSTS +RSYNC_REMOTE_PATH +REMOTE_HOST_PING +REMOTE_3RD_PARTY_HOSTS +SUDO_EXEC +SQL_USER +DATABASES_ALL +DATABASES_ALL_EXCLUDE_LIST +DATABASES_LIST +SOFT_MAX_EXEC_TIME_DB_TASK +HARD_MAX_EXEC_TIME_DB_TASK +MYSQLDUMP_OPTIONS +COMPRESSION_LEVEL +DIRECTORY_LIST +RECURSIVE_DIRECTORY_LIST +RECURSIVE_EXCLUDE_LIST +RSYNC_PATTERN_FIRST +RSYNC_INCLUDE_PATTERN +RSYNC_EXCLUDE_PATTERN +RSYNC_INCLUDE_FROM +RSYNC_EXCLUDE_FROM +PATH_SEPARATOR_CHAR +PRESERVE_PERMISSIONS +PRESERVE_OWNER +PRESERVE_GROUP +PRESERVE_EXECUTABILITY +PRESERVE_ACL +PRESERVE_XATTR +COPY_SYMLINKS +KEEP_DIRLINKS +PRESERVE_HARDLINKS +RSYNC_COMPRESS +SOFT_MAX_EXEC_TIME_FILE_TASK +HARD_MAX_EXEC_TIME_FILE_TASK +PARTIAL +DELETE_VANISHED_FILES +DELTA_COPIES +BANDWIDTH +RSYNC_EXECUTABLE +DESTINATION_MAILS +SENDER_MAIL +SMTP_SERVER +SMTP_PORT +SMTP_ENCRYPTION +SMTP_USER +SMTP_PASSWORD +SOFT_MAX_EXEC_TIME_TOTAL +HARD_MAX_EXEC_TIME_TOTAL +KEEP_LOGGING +ROTATE_SQL_BACKUPS +ROTATE_SQL_COPIES +ROTATE_FILE_BACKUPS +ROTATE_FILE_COPIES +LOCAL_RUN_BEFORE_CMD +LOCAL_RUN_AFTER_CMD +REMOTE_RUN_BEFORE_CMD +REMOTE_RUN_AFTER_CMD +MAX_EXEC_TIME_PER_CMD_BEFORE +MAX_EXEC_TIME_PER_CMD_AFTER +STOP_ON_CMD_ERROR +RUN_AFTER_CMD_ON_ERROR +) + +VALUES=( +test-backup +'' +yes +yes +local +/home/storage/sql +/home/storage/files +no +/home/storage/crypt +'Your Name used with GPG signature' +yes +yes +1024 +yes +1048576 +1048576 +ssh://backupuser@remote.system.tld:22/ +${HOME}/.ssh/id_rsa +yes +no +'' +yes +'www.kernel.org www.google.com' +no +root +yes +test +'' +3600 +7200 +'--opt --single-transaction' +3 +/some/path +/home +/home/backupuser\;/host/lost+found +include +'' +'' +'' +'' +\; +yes +yes +yes +yes +no +no +yes +yes +no +no +3600 +7200 +no +no +yes +0 +rsync +infrastructure@example.com +sender@example.com +smtp.isp.tld +25 +none +'' +'' +30000 +36000 +1801 +no +7 +no +7 +'' +'' +'' +'' +0 +0 +no +no +) + function Usage { echo "$PROGRAM $PROGRAM_BUILD" echo $AUTHOR @@ -42,7 +211,7 @@ function LoadConfigFile { fi } -function RewriteConfigFiles { +function RewriteOldConfigFiles { local config_file="${1}" if ((! grep "BACKUP_ID=" $config_file > /dev/null) && ( ! grep "INSTANCE_ID=" $config_file > /dev/null)); then @@ -65,18 +234,6 @@ function RewriteConfigFiles { sed -i'.tmp' 's/^LOCAL_SQL_STORAGE=/SQL_STORAGE=/g' "$config_file" sed -i'.tmp' 's/^LOCAL_FILE_STORAGE=/FILE_STORAGE=/g' "$config_file" - if ! grep "^ENCRYPTION=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^FILE_STORAGE=*/a\'$'\n''ENCRYPTION=no\'$'\n''' "$config_file" - fi - - if ! grep "^CRYPT_STORAGE=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^ENCRYPTION=*/a\'$'\n''CRYPT_STORAGE=/home/storage/backup/crypt\'$'\n''' "$config_file" - fi - - if ! grep "^GPG_RECIPIENT=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^CRYPT_STORAGE=*/a\'$'\n''GPG_RECIPIENT=John Doe\'$'\n''' "$config_file" - fi - sed -i'.tmp' 's/^DISABLE_GET_BACKUP_FILE_SIZE=no/GET_BACKUP_SIZE=yes/g' "$config_file" sed -i'.tmp' 's/^DISABLE_GET_BACKUP_FILE_SIZE=yes/GET_BACKUP_SIZE=no/g' "$config_file" sed -i'.tmp' 's/^LOCAL_STORAGE_KEEP_ABSOLUTE_PATHS=/KEEP_ABSOLUTE_PATHS=/g' "$config_file" @@ -124,95 +281,28 @@ function RewriteConfigFiles { sed -i'.tmp' '/^INSTANCE_ID=*/a\'$'\n''BACKUP_TYPE=local\'$'\n''' "$config_file" fi fi +} - # Add new config values from v1.1 if they don't exist - if ! grep "^CREATE_DIRS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^ENCRYPTION=*/a\'$'\n''CREATE_DIRS=yes\'$'\n''' "$config_file" - fi +function AddMissingConfigOptions { + local config_file="${1}" + local counter=0 - if ! grep "^GET_BACKUP_SIZE=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^BACKUP_SIZE_MINIMUM=*/a\'$'\n''GET_BACKUP_SIZE=yes\'$'\n''' "$config_file" - fi + while [ $counter -lt ${#KEYWORDS[@]} ]; do + if ! grep "^${KEYWORDS[$counter]}" "$config_file" > /dev/null; then + #sed -i'.tmp' '/^ROTATE_SQL_COPIES=*/a\'$'\n''ROTATE_FILE_COPIES='$VALUE'\'$'\n''' "$config_file" + sed -i'.tmp' '/^'${KEYWORDS[$((counter-1))]}'=*/a\'$'\n'${KEYWORDS[$counter]}'="'"${VALUES[$counter]}"'"\'$'\n''' "$config_file" + if [ $? != 0 ]; then + echo "Cannot add missing ${[KEYWORDS[$counter]}." + exit 1 + fi + echo "Added missing ${KEYWORDS[$counter]} config option with default option [${VALUES[$counter]}]" + fi + counter=$((counter+1)) + done +} - if ! grep "^MYSQLDUMP_OPTIONS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^HARD_MAX_EXEC_TIME_DB_TASK=*/a\'$'\n''MYSQLDUMP_OPTIONS="--opt --single-transaction"\'$'\n''' "$config_file" - fi - - if ! grep "^RSYNC_REMOTE_PATH=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^SSH_COMPRESSION=*/a\'$'\n''RSYNC_REMOTE_PATH=\'$'\n''' "$config_file" - fi - - if ! grep "^SSH_IGNORE_KNOWN_HOSTS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^SSH_COMPRESSION=*/a\'$'\n''SSH_IGNORE_KNOWN_HOSTS=no\'$'\n''' "$config_file" - fi - - if ! grep "^REMOTE_HOST_PING=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^RSYNC_REMOTE_PATH=*/a\'$'\n''REMOTE_HOST_PING=yes\'$'\n''' "$config_file" - fi - - if ! grep "^COPY_SYMLINKS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PRESERVE_XATTR=*/a\'$'\n''COPY_SYMLINKS=yes\'$'\n''' "$config_file" - fi - - if ! grep "^KEEP_DIRLINKS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^COPY_SYMLINKS=*/a\'$'\n''KEEP_DIRLINKS=yes\'$'\n''' "$config_file" - fi - - if ! grep "^PRESERVE_HARDLINKS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^KEEP_DIRLINKS=*/a\'$'\n''PRESERVE_HARDLINKS=no\'$'\n''' "$config_file" - fi - - if ! grep "^RSYNC_PATTERN_FIRST=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^RECURSIVE_EXCLUDE_LIST=*/a\'$'\n''RSYNC_PATTERN_FIRST=include\'$'\n''' "$config_file" - fi - - if ! grep "^RSYNC_INCLUDE_PATTERN=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^RSYNC_EXCLUDE_PATTERN=*/a\'$'\n''RSYNC_INCLUDE_PATTERN=""\'$'\n''' "$config_file" - fi - - if ! grep "^PRESERVE_PERMISSIONS=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PATH_SEPARATOR_CHAR=*/a\'$'\n''PRESERVE_PERMISSIONS=yes\'$'\n''' "$config_file" - fi - - if ! grep "^PRESERVE_OWNER=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PRESERVE_PERMISSIONS=*/a\'$'\n''PRESERVE_OWNER=yes\'$'\n''' "$config_file" - fi - - if ! grep "^PRESERVE_GROUP=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PRESERVE_OWNER=*/a\'$'\n''PRESERVE_GROUP=yes\'$'\n''' "$config_file" - fi - - if ! grep "^PRESERVE_EXECUTABILITY=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PRESERVE_GROUP=*/a\'$'\n''PRESERVE_EXECUTABILITY=yes\'$'\n''' "$config_file" - fi - - if ! grep "^PARTIAL=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^HARD_MAX_EXEC_TIME_FILE_TASK==*/a\'$'\n''PARTIAL=no\'$'\n''' "$config_file" - fi - - if ! grep "^DELETE_VANISHED_FILES=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PARTIAL=*/a\'$'\n''DELETE_VANISHED_FILES=no\'$'\n''' "$config_file" - fi - - if ! grep "^DELTA_COPIES=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^PARTIAL=*/a\'$'\n''DELTA_COPIES=yes\'$'\n''' "$config_file" - fi - - if ! grep "^BANDWIDTH=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^DELTA_COPIES=*/a\'$'\n''BANDWIDTH=0\'$'\n''' "$config_file" - fi - - if ! grep "^KEEP_LOGGING=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^HARD_MAX_EXEC_TIME_TOTAL=*/a\'$'\n''KEEP_LOGGING=1801\'$'\n''' "$config_file" - fi - - if ! grep "^STOP_ON_CMD_ERROR=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^MAX_EXEC_TIME_PER_CMD_AFTER=*/a\'$'\n''STOP_ON_CMD_ERROR=no\'$'\n''' "$config_file" - fi - - if ! grep "^RUN_AFTER_CMD_ON_ERROR=" "$config_file" > /dev/null; then - sed -i'.tmp' '/^STOP_ON_CMD_ERROR=*/a\'$'\n''RUN_AFTER_CMD_ON_ERROR=no\'$'\n''' "$config_file" - fi +function UpdateConfigHeader { + local config_file="${1}" # "onfig file rev" to deal with earlier variants of the file sed -i'.tmp' '/onfig file rev/c\###### '$SUBPROGRAM' config file rev '$PROGRAM_BUILD "$config_file" @@ -225,7 +315,9 @@ if [ "$1" != "" ] && [ -f "$1" ] && [ -w "$1" ]; then # Make sure there is no ending slash CONF_FILE="${CONF_FILE%/}" LoadConfigFile "$CONF_FILE" - RewriteConfigFiles "$CONF_FILE" + RewriteOldConfigFiles "$CONF_FILE" + AddMissingConfigOptions "$CONF_FILE" + UpdateConfigHeader "$CONF_FILE" else Usage fi