Atualizei o Script
Agora vamos fazer backup do dia impar e do dia par :
# Preparacao das variaveis.
###########################
# Atribui o ano atual a variavel ano.
ano=$(date +%Y)
# Atribui o mes atual a variavel mes.
mes=$(date +%m)
# Atribui o dia atual a variavel dia.
dia=$(date +%d)
# Atribui a hora atual a variavel hora.
hora=$(date +%H)
# Atribui o minuto atual a variavel minuto.
minuto=$(date +%M)
# Atribui o hostname atual a variavel host
host=$(hostname)
# copia os arquivos para o diretorio de backup.
# cp teste1.txt ${host}_${ORACLE_SID}_${dia}${mes}${ano}${hora}${minuto}
today=$(date +'%a')
if [[ $today == "Mon" || $today == "Wed" || $today == "Fri" || $today == "Sun" ]]; then
echo " ********************************* "
echo " *** *** "
echo " *** Backup Tipo Dia PAR *** "
echo " *** *** "
echo " ********************************* "
diretorio='/bkpdb/diapar'
else
echo " ********************************* "
echo " *** *** "
echo " *** Backup Tipo Dia IMPAR *** "
echo " *** *** "
echo " ********************************* "
diretorio='/bkpdb/diaimpar'
fi
# Remove todos os arquivos no diretório designado
rm -rf "${diretorio}"/*.*
sql_database=$(mysql -udb_backup -hlocalhost -pbackup@2000 -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -u<<USUARIO BACKUP>> -p<<SENHA USUARIO BACKUP>> --databases $db > "${diretorio}"/$db.sql
echo "Compactando Banco $db.sql ";
gzip "${diretorio}"/$db.sql "${diretorio}"/
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf "${diretorio}"/$db.sql
done
echo " ********************************* "
echo " *** *** "
echo " *** MANUTENÇÃO DIARIA *** "
echo " *** *** "
echo " ********************************* "
mysqlcheck -u<<USUARIO BACKUP>> -p<<SENHA USUARIO BACKUP>> --auto-repair --all-databases
* Lembrando que para devemos realizar o mapeamento do diretorio de backup exemplo :
mount.cifs //<<IP DO SERVIDOR>>/COMPARTILHAMENTO /bkpdb -o vers=2.0,user=<<USUARIO COM PERMISSAO NO MAPEAMENTO>>,pass='<<SENHA USUARIO COM PERMISSAO NO MAPEAMENTO>>',file_mode=0777,dir_mode=0777,iocharset=utf8
Lembrando :
Export:
mysqldump -u username –-password=your_password database_name >file.sql
Import:
mysql -u username –-password=your_password database_name <file.sql
OBS :
Evite usar o root, crie um usuário só para o backup.
lembrando que o script mapeia outro servidor para guarda os arquivos de backup ( /bkpdb/diretório dia da semana )
# Preparacao das variaveis.
###########################
# Atribui o ano atual a variavel ano.
ano=$(date +%Y)
# Atribui o mes atual a variavel mes.
mes=$(date +%m)
# Atribui o dia atual a variavel dia.
dia=$(date +%d)
# Atribui o hora atual a variavel hora.
hora=$(date +%H)
# Atribui o minuto atual a variavel minuto.
minuto=$(date +%M)
# Atribui o hostname atual a variavel HOST
host=$('hostname')
# copia os arquivos para o diretorio de backup.
# cp teste1.txt $host'_'$ORACLE_SID'_'$dia$mes$ano$hora$minuto
DIA=`date +'%w'`
clear
cont=0
if [ $DIA -eq 0 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE domingo *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/domingo/$db.sql
gzip -f /bkpdb/domingo/$db.sql /bkpdb/domingo/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/domingo/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS domingo *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi
if [ $DIA -eq 1 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE segunda *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/segunda/$db.sql
gzip -f /bkpdb/segunda/$db.sql /bkpdb/segunda/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/segunda/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS SEGUNDA *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi
if [ $DIA -eq 2 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE terca *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/terca/$db.sql
gzip -f /bkpdb/terca/$db.sql /bkpdb/terca/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/terca/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS terca *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi
if [ $DIA -eq 3 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE quarta *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/quarta/$db.sql
gzip -f /bkpdb/quarta/$db.sql /bkpdb/quarta/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/quarta/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS quarta *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi
if [ $DIA -eq 4 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE quinta *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/quinta/$db.sql
gzip -f /bkpdb/quinta/$db.sql /bkpdb/quinta/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/quinta/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS quinta *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi
if [ $DIA -eq 5 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE sexta *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/sexta/$db.sql
gzip -f /bkpdb/sexta/$db.sql /bkpdb/sexta/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/sexta/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS sexta *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi
if [ $DIA -eq 6 ]; then
clear
echo "."
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** REALIZANDO BACKUP DE sabado *** "
echo " *** SERVIDOR *** "
echo " *** *** "
echo " *** << SERVIDOR >> *** "
echo " *** *** "
echo " *********************************************** "
echo "."
echo "."
sql_database=$(mysql -uroot -hlocalhost -'<< SENHA >>' -Bse 'SHOW DATABASES WHERE `database` != "information_schema" AND `database` != "mysql"')
for db in ${sql_database}; do
cont=$(( $cont + 1 ))
echo "Processando Banco N. $(( $cont + 1 )) $db.sql ";
mysqldump -hlocalhost -uroot -'<< SENHA >>' --databases $db > /bkpdb/sabado/$db.sql
gzip -f /bkpdb/sabado/$db.sql /bkpdb/sabado/$db_$(date +%Y_%m_%d).zip
echo "Deletando Arquivo Nao Compactando Banco $db.sql ";
rm -rf /bkpdb/sabado/$db.sql
echo "."
echo " *********************************************** "
echo " *** *** "
echo " *** MANUTENCAO SEMANAL TODOS OS sabado *** "
echo " *** *** "
echo " *********************************************** "
done
mysqlcheck -u root -'<< SENHA >>' --auto-repair --optimize --all-databases
exit
fi