Commit 5bff5f1ad48cedc971ab59fd78ee5b09bfab5a7f
1 parent
45b6b00a
Locality abstraction for db-backup and db-restore
Showing
1 changed file
with
77 additions
and
16 deletions
cluster-manager.sh
| ... | ... | @@ -2,7 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | # The MIT License (MIT) |
| 4 | 4 | # |
| 5 | -# Copyright (c) <year> <copyright holders> | |
| 5 | +# Copyright (c) 2015 ISISLab, Università degli Studi di Salerno | |
| 6 | 6 | # |
| 7 | 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | 8 | # of this software and associated documentation files (the "Software"), to deal |
| ... | ... | @@ -23,6 +23,62 @@ |
| 23 | 23 | # THE SOFTWARE. |
| 24 | 24 | |
| 25 | 25 | # |
| 26 | +# FUNCTIONS DEFINITIONS | |
| 27 | +# | |
| 28 | + | |
| 29 | +# host_exec_cmd | |
| 30 | +# | |
| 31 | +# Description: | |
| 32 | +# Calls a command on a host, abstracting the locality of the host. | |
| 33 | +# | |
| 34 | +# Parameters: | |
| 35 | +# $1 hostname where to perform the command | |
| 36 | +# $2 the command to execute | |
| 37 | +function host_exec_cmd { | |
| 38 | + if [ "$MYHOST" == "$1" ]; then | |
| 39 | + "$2" | |
| 40 | + else | |
| 41 | + ssh -t ${SSH_USER}${1}.routetopa.eu "$2" | |
| 42 | + fi | |
| 43 | +} | |
| 44 | + | |
| 45 | +# host_send_file | |
| 46 | +# | |
| 47 | +# Description: | |
| 48 | +# Send a local file to remote host. If the copy is issued on the local host | |
| 49 | +# itself, the command is ignored. | |
| 50 | +# | |
| 51 | +# Parameters: | |
| 52 | +# $1 hostname to copy the file to | |
| 53 | +# $2 local filename | |
| 54 | +# $3 remote localhost | |
| 55 | +function host_send_file { | |
| 56 | + if [ "$MYHOST" == "$1" ]; then | |
| 57 | + mv ${2} ${3} | |
| 58 | + else | |
| 59 | + scp ${2} ${SSH_USER}${1}.routetopa.eu:${2} | |
| 60 | + fi | |
| 61 | +} | |
| 62 | + | |
| 63 | +# host_recv_file | |
| 64 | +# | |
| 65 | +# Description: | |
| 66 | +# Receive a remote file to localhost host. If the copy is issued on the local host | |
| 67 | +# itself, the command is ignored. | |
| 68 | +# | |
| 69 | +# Parameters: | |
| 70 | +# $1 hostname to copy the file from | |
| 71 | +# $2 remote filename | |
| 72 | +# $3 local filename | |
| 73 | +function host_recv_file { | |
| 74 | + if [ "$MYHOST" == "$1" ]; then | |
| 75 | + mv ${2} ${3} | |
| 76 | + else | |
| 77 | + scp ${SSH_USER}${1}.routetopa.eu:${2} ./${3} | |
| 78 | + fi | |
| 79 | +} | |
| 80 | + | |
| 81 | +# | |
| 26 | 82 | # DEFAULT OPTIONS |
| 27 | 83 | # |
| 28 | 84 | |
| ... | ... | @@ -35,6 +91,7 @@ MYSQL_PASS=is15rdc |
| 35 | 91 | MYSQL_DB=oxwall |
| 36 | 92 | |
| 37 | 93 | HOSTLIST=( |
| 94 | + 'spod' | |
| 38 | 95 | 'prato' |
| 39 | 96 | 'dublin' |
| 40 | 97 | 'issy' |
| ... | ... | @@ -101,31 +158,35 @@ echo 'NOTE: THIS ACTION HAS NOT BEEN TESTED YET!! JUST ECHOING COMMANDS...' |
| 101 | 158 | echo ssh -t ${SSH_USER}${HOST}.routetopa.eu "source $ARG_SCRIPT_NAME; rm $ARG_SCRIPT_NAME" |
| 102 | 159 | done |
| 103 | 160 | ;; |
| 104 | - db-restore) | |
| 161 | + db-backup) | |
| 105 | 162 | ARG_BACKUP_NAME=$2 |
| 106 | 163 | if [ -z $ARG_BACKUP_NAME ]; then |
| 107 | - echo "ERROR: Please specify the name of the backup. e.g.: $0 backup.sql" | |
| 108 | - exit 1 | |
| 109 | - fi | |
| 110 | - if [ ! -f $ARG_BACKUP_NAME ]; then | |
| 111 | - echo "ERROR: File $ARG_BACKUP_NAME does not exists" | |
| 112 | - exit 1 | |
| 164 | + ARG_BACKUP_NAME="${MYSQL_DB}-$(date +%Y%m%d-%H%M%S).sql" | |
| 165 | + echo "No given backup filename. Using $ARG_BACKUP_NAME" | |
| 113 | 166 | fi |
| 114 | 167 | for HOST in ${HOSTLIST[@]}; do |
| 115 | - scp $ARG_BACKUP_NAME ${SSH_USER}${HOST}.routetopa.eu:$ARG_BACKUP_NAME | |
| 116 | - ssh -t ${SSH_USER}${HOST}.routetopa.eu "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${ARG_BACKUP_NAME}" | |
| 168 | + host_exec_cmd ${HOST} "mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} > ${ARG_BACKUP_NAME}" | |
| 169 | + host_recv_file ${HOST} ${ARG_BACKUP_NAME} ${HOST}-${ARG_BACKUP_NAME} | |
| 117 | 170 | done |
| 118 | - db-backup) | |
| 171 | + ;; | |
| 172 | + db-restore) | |
| 119 | 173 | ARG_BACKUP_NAME=$2 |
| 120 | - if [ -z $ARG_BACKUP_NAME ]; then | |
| 121 | - ARG_BACKUP_NAME="${MYSQL_DB}-$(date +%Y%m%d-%H%M%S).sql" | |
| 122 | - echo "No given backup filename. Using $ARG_BACKUP_NAME" | |
| 174 | + if [ -z ${ARG_BACKUP_NAME} ]; then | |
| 175 | + echo "ERROR: Please specify the name of the backup. e.g.: $0 backup.sql" | |
| 176 | + exit 1 | |
| 123 | 177 | fi |
| 124 | 178 | for HOST in ${HOSTLIST[@]}; do |
| 125 | - ssh -t ${SSH_USER}${HOST}.routetopa.eu "mysqldump -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} > ${ARG_BACKUP_NAME}" | |
| 126 | - scp ${SSH_USER}${HOST}.routetopa.eu:${ARG_BACKUP_NAME} ./${HOST}-${ARG_BACKUP_NAME} | |
| 179 | + if [ ! -f "${HOST}-${ARG_BACKUP_NAME}" ]; then | |
| 180 | + echo "ERROR: File ${HOST}-${ARG_BACKUP_NAME} does not exists" | |
| 181 | + exit 1 | |
| 182 | + fi | |
| 183 | + done | |
| 184 | + for HOST in ${HOSTLIST[@]}; do | |
| 185 | + host_send_file ${HOST} "${HOST}-${ARG_BACKUP_NAME}" "${ARG_BACKUP_NAME}" | |
| 186 | + host_exec_cmd ${HOST} "mysql -u${MYSQL_USER} -p${MYSQL_PASS} ${MYSQL_DB} < ${ARG_BACKUP_NAME}" | |
| 127 | 187 | done |
| 128 | 188 | ;; |
| 189 | + | |
| 129 | 190 | db-sql) |
| 130 | 191 | echo 'NOTE: THIS ACTION HAS NOT BEEN TESTED YET!! JUST ECHOING COMMANDS...' |
| 131 | 192 | ARG_SCRIPT_NAME=$2 | ... | ... |