Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script para realizar una copia de seguridad de la base de datos y de los archivos de Cacti, compactarla, crear copias históricas y transferirlas a servidores remotos mediante ftp.
#!/bin/bash
# Realiza una copia de seguridad completa de la herramienta.
# DEFINICION DE CONSTANTES.
# =========================
DIA=`date +"%Y%m%d"`
HORA=`date +"%H%M"`
# VARIABLES DE CONFIGURACION.
# ===========================
# -- Configuraciones de la aplicacion.
APP_NAME=Cacti
APP_HOME=/usr/share/cacti
APP_DB_TYPE=mysql #[pg|mysql]
APP_DB_NAME=cacti
APP_DB_USER=cactiuser
APP_DB_PASS=cactiuser
APP_RRD_HOME=$APP_HOME/rra
APP_BACKUP_HOME=$APP_HOME/backups
APP_BACKUP_DB=$APP_BACKUP_HOME/cacti_db.sql
APP_BACKUP_RRD=$APP_BACKUP_HOME/rrd
# -- Configuraciones de backup
BACKUP_PATH=/backups/cacti
BACKUP_NAME="cacti_"$DIA"_"$HORA".tar.gz"
BACKUP_HISTO="cacti_histo_"$DIA"_"$HORA".tar.gz"
BACKUP_LIVE_TIME=30
BACKUP_HISTO_LIVE_TIME=365
# -- FTP
FTP_SERVER=ip_o_dominio_servidor
FTP_USER=userbkp
FTP_PASS=userbkp
FTP_REMOTE_CD=remote_backups
FTP_LOCAL_CD=$BACKUP_PATH
FTP_BACKUP_NAME=cacti.tar.gz
# -- Patron limpieza CACTI
PATRON_LIMPIEZA=cacti_*.tar.gz
PATRON_LIMPIEZA_HISTO=cacti_histo_*.tar.gz
# SECCION DE PROCEDIMIENTOS BACKUP.
# =================================
# -- Comprobacion y creacion de directorios de backup.
echo
echo "-->Verificacion de directorios de Backup"
echo "===================================================================="
if [ ! -d $APP_BACKUP_RRD ]; then
echo -n "Creando directorio $APP_BACKUP_RRD: "
mkdir -p $APP_BACKUP_RRD
echo "OK"
sleep 1
else
echo "Directorio $APP_BACKUP_RRD: OK"
fi
if [ ! -d $BACKUP_PATH ]; then
echo -n "Creando directorio $BACKUP_PATH: "
mkdir -p $BACKUP_PATH
echo "OK"
sleep 1
else
echo "Directorio $BACKUP_PATH: OK"
fi
# -- Segun el motor de base de datos especificado se procede a exportar la base de datos.
echo
echo "-->BACKUP DE LA BASE DE DATOS DE $APP_NAME"
echo "===================================================================="
if [ $APP_DB_TYPE == "mysql" ]; then
echo "Exportando la base de datos de $APP_NAME a partir de MySQL. Aguarde un momento..."
mysqldump -v -u $APP_DB_USER -p$APP_DB_PASS --database $APP_DB_NAME > $APP_BACKUP_DB
echo "Fin del proceso de exportacion."
echo
elif [ $APP_DB_TYPE == "pg" ]; then
echo "Exportando la base de datos de $APP_NAME a partir de PostgreSQL. Aguarde un momento..."
export PGUSER=$APP_DB_USER
export PGPASSWORD=$APP_DB_PASS
pg_dump -b -F p --column-inserts $APP_DB_NAME > $APP_BACKUP_DB
unset PGUSER
unset PGPASSWORD
echo "Fin del proceso de exportacion."
fi
# -- Backup de la base de datos RRDTOOL.
echo
echo "-->BACKUP DE LAS BASES DE DATOS RRDTOOL DE $APP_NAME"
echo "===================================================================="
LISTA=`ls -1 $APP_RRD_HOME/*.rrd`
for i in $LISTA; do
echo $i | awk '{print "rrdtool dump "$1" > "$1".xml"}' | sh -x
done
mv -vf $APP_RRD_HOME/*.xml $APP_BACKUP_RRD
# Se generan los archivos con todas las copias de seguridad para ser enviadas a una unidad de cinta.
echo
echo "-->CREANDO FULL BACKUP DE $APP_NAME"
echo "===================================================================="
cd $APP_BACKUP_HOME
tar -czvf $BACKUP_PATH/$BACKUP_NAME *
# Almacenando una copia de seguridad alternativa si la fecha corresponde a fin de mes.
TOMORROW=`date --date=tomorrow +%d`
if [ $TOMORROW -eq "1" ]; then
echo
echo "-->CREANDO BACKUP HISTORICO POR SER FIN DE MES"
echo "===================================================================="
cp -v $BACKUP_PATH/$BACKUP_NAME $BACKUP_PATH/$BACKUP_HISTO
fi
# Para no llenar el disco duro con backups, se eliminan todos los backups diarios pasados la cantidad
# de dias especificados en la variable BACKUP_LIVE_TIME, lo backups historicos se eliminan luego de la
# cantidad de dias especificados en la variable BACKUP_HISTO_LIVE_TIME.
echo
echo "-->LIMPIEZA DE BACKUPS ANTIGUOS"
echo "===================================================================="
find $BACKUP_PATH/$PATRON_LIMPIEZA -mtime +$BACKUP_LIVE_TIME -exec rm {} \;
find $BACKUP_PATH/$PATRON_LIMPIEZA_HISTO -mtime +$BACKUP_HISTO_LIVE_TIME -exec rm {} \;
# Se copia el full backup a un servidor de respaldo.
echo
echo "-->COPIANDO EL FULL BACKUP DE $APP_NAME A UN SERVIDOR DE RESPALDO"
echo "===================================================================="
/usr/bin/ftp -n <<EOD
open $FTP_SERVER
quote USER $FTP_USER
quote PASS $FTP_PASS
bin
cd $FTP_REMOTE_CD
lcd $FTP_LOCAL_CD
del $FTP_BACKUP_NAME
put $BACKUP_NAME
rename $BACKUP_NAME $FTP_BACKUP_NAME
bye
EOD
@cristobal85

This comment has been minimized.

Copy link

cristobal85 commented Jul 14, 2015

Good work! Thanks so much.

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.