I have created a script for linux that allows you to backup the DB by saving it in a compressed file with a date and time of elaboration.
It is also sent by mail automatically and it deletes the files ".bz2" (which is the format used to compress the backup) that is older than 30 days.
I want to improve the script that recommends me for it:
#!/bin/bash
export FECHA=($(date +"%d-%m-%Y_%H:%M:%S") )# fecha de elaboración
export NAME=RespaldoBDSigefirrhh_${FECHA}.backup # nombre del respaldo
export DIR=/home/sigefirrhh/backup/ #direccion donde se guarda los documentos
USER_DB=admin #usuario de la BD
NAME_DB=nombre # nombre de la BD
[email protected] #direccion de correo a enviar
MESSAGE_FILE=backup.mail.message # colocar este archivo en DIR.
The "MESSAGE_FILE=backup.mail.message"
is a document type txt that contains a message or a description to be placed within the mail to be sent, this document is placed in the same place where the database backup will be so that it is better.
cd $DIR > ${NAME} #se habre direccion donde se guardara el backup
export PGPASSWORD=xxxx #clave de la BD
chmod 777 ${NAME} #se le da permisos al archivo
echo "procesando la copia de la base de datos" #mensaje a ver
pg_dump -i -h (ip del servidor) -p (puerto) -U $USER_DB -F c -b -v -f ${NAME} $NAME_DB # comando para crear el backup
echo "backup terminado" #mensaje a ver
# usamos bzip2 para comprimir el sql
bzip2 ${NAME}
Now we send the backup by mail using mutt, the code gets inside a until
to create a cycle so that it persists until sending it successfully.
# Enviar correo
until
mutt -s "Copia de seguridad BD ${NAME_DB}: $(date +"%d-%m-%Y")" ${USER_GMAIL} -a ${NAME}.bz2 < ${DIR}${MESSAGE_FILE}
do echo " NO SE ENVIO CORREO REINTENTANDO ... " :; done
echo "SE ENVIO CORREO"
#Elimina archivos mayor a 30 dias
find ${DIR}*.bz2 -mtime +30 -exec rm {} \;
Later a message is shown if it can not be sent and it enters the cycle and when it is sent it shows another message.
Finally, you have the code to delete the .bz2 tablets that are more than 30 days old at the given address