Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exemplo de uso do bash do linux para criar uma função que realiza do DUMP de todas as tabelas de um servidor mysql.
#!/bin/bash
#Variaveis de apoio
#IP ou URL do Servidor MySQL
SERVER_MYSQL = 172.1.22.49
#Usuario com os privilegios de SUPER, USAGE, EXECUTE e FILE
USER ="usrbkp"
PWD = "senha"
#Arquivo temporario
FILE_TEMP = "databases"
#Diretório onde os backups residem
BKP_DIR="/srv/backups/mysql/"
#Data do Backup
NW_DATE = $(date "+%Y%m%d")
#Qtd de backups ultimos 3 arquivos
MAX_FILES = 3
#Nome do arquivo final compactado
FINAL_FILE_PREFIX = "backup-"
function backup()
{
echo "Inicializando rotina de backup do Servidor Mysql $SERVER_MYSQL"
mysqldump -u$USER -p$PWD -h$SERVER_MYSQL --add-drop-table --quote-names --all-databases --add-drop-database > "/tmp/$FILE_TEMP-$NW_DATE.sql"
echo "Compactando o arquivo ..."
gzip -f "/tmp/$FILE_TEMP-$NW_DATE.sql"
if [ ! -d $BKP_DIR ]; then
mkdir $BKP_DIR
fi
cp -f "/tmp/$FILE_TEMP-$NW_DATE.gz" "$BKP_DIR/$FILE_TEMP-$NW_DATE.gz"
a=0
b=$(ls -t $BKP_DIR)
c=$MAX_FILES
for arq in $b; do
a=$(($a+1))
if [ "$a" -gt $c ]; then
rm -f "$BKP_DIR/$arq"
fi
done
}
backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.