Skip to content

Instantly share code, notes, and snippets.

@FernandoBasso
Last active October 15, 2017 10:25
Show Gist options
  • Save FernandoBasso/d85970307b65dd8115ff to your computer and use it in GitHub Desktop.
Save FernandoBasso/d85970307b65dd8115ff to your computer and use it in GitHub Desktop.
failed_dbs='failed-databases.txt';
errors_file='errors-backup.txt';
dirdate=$(date +'%Y-%m-%d');
# site host user pass dbname
hosts=(
'projeto1 foo.bar.net prjeto1user 1234 projeto1dbname'
'projeto2 foo.bar.net prjeto2user 1234 projeto2dbname'
'projeto3 foo.bar.net prjeto3user 1234 projeto3dbname'
)
mkdir "$dirdate";
printf "Lista de bancos NÃO salvos ---:\n\n\n" > "${dirdate}/${failed_dbs}";
printf "Mensagens de erro ------------:\n\n" > "${dirdate}/${errors_file}";
for ((i=0; i < "${#hosts[@]}"; ++i)); do
# Turns string into array.
read -a cur <<< "${hosts[i]}";
hourmin=$(date +'%H-%m');
# Creates the filename: "yyyy-mm-dd_hh-mm-nome_do_site.sql"
file="${dirdate}_${hourmin}-${cur[0]}.sql";
printf "Salvando ${cur[0]}...";
# Dumps the DB to .sql file and errors to a log file.
mysqldump -h "${cur[1]}" -u "${cur[2]}" -p"${cur[3]}" "${cur[4]}" > "${dirdate}/${file}" 2>> "${dirdate}/${errors_file}";
# If there was an error
if [[ $? != 0 ]]; then
# Remove the sql file of the one that just failed.
rm "${dirdate}/${file}";
# Tells the user that there was an error.
printf "\e[01;31m %40s" "--> \"${cur[0]}\" ERROR..."; echo -e "\e[0m";
# Stores the names of the db that could not be dumped.
printf "\t${cur[0]}\n" >> "${dirdate}/${failed_dbs}";
else
# Tells the ones that succeeded.
printf "\e[01;32m %37s" "--> \"${cur[0]}\" OK!!!"; echo -e "\e[0m";
fi;
done
printf "\n\nProcesso finalizado.\n\n";
printf "Bancos que falharam ............: \e[01;35m${dirdate}/${failed_dbs}\e[0m.\n\n";
printf "Informacoes sobre os erros .....: \e[01;35m${dirdate}/${errors_file}\e[0m.\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment