Created
January 7, 2022 21:48
-
-
Save manuelcanga/bf335fc0515d16afae0e075e6254806e to your computer and use it in GitHub Desktop.
Create app backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#set -x | |
INSTANCIA=$1 | |
TODAY=$(date -I) | |
### ------------------------------------------------------------ | |
### Constants. | |
### ------------------------------------------------------------ | |
BACKUP_DIR="/backup/app_258" | |
WWW_DIR="/srv/www/htdocs" | |
# Tiempo que se conservaran los backup, indicado en numeros de dias. | |
# 0 : Infinito | |
# n : Se eliminaran los ficheros creados hace n dias. | |
EXPIRE_TIME=7 | |
# TODO: Esto deber?a de obtenerse de los ficheros de configuracion. | |
APP_HOST_DB="localhost" | |
APP_NAME_DB="app_cr_258" | |
APP_USER_DB="app" | |
APP_PASSWD_DB="Ao3Z7mNV" | |
APP_DB_FILE="instancia-$INSTANCIA-$TODAY-BBDD.sql" | |
APP_FS_INC_FILE="instancia-$INSTANCIA-$TODAY-DIR_inc.tgz" | |
APP_FS_FULL_FILE="instancia-$INSTANCIA-$TODAY-DIR.tgz" | |
TAR_INCREMENTAL_FILE="snapshot_week_$INSTANCIA" | |
INC_NEW=10 | |
INC_EXISTS=11 | |
### ------------------------------------------------------------ | |
### Utils. | |
### ------------------------------------------------------------ | |
MYSQLDUMP=$(which mysqldump) | |
GZIP=$(which gzip) | |
MKDIR=$(which mkdir) | |
TAR=$(which tar) | |
DATE=$(which date) | |
FIND=$(which find) | |
## return: $1: with incremental filename (path included). | |
## exit if fail. | |
function check_incremental_file() { | |
inc_file_n=$(find $BACKUP_DIR -maxdepth 1 -iname *.current | wc -l) | |
case $inc_file_n in | |
"0") | |
filename=$TAR_INCREMENTAL_FILE.$TODAY.current | |
eval $1=$BACKUP_DIR/$filename | |
echo "++ check_incremental_file() return no file." | |
return $INC_NEW | |
;; | |
"1") | |
filename=$(find $BACKUP_DIR -maxdepth 1 -iname *.current) | |
eval $1=$filename | |
echo "++ check_incremental_file() found. [$filename]" | |
return $INC_EXISTS | |
;; | |
*) | |
echo "** ERROR: Existe m?s de un archivo de control incremental." | |
exit -1 | |
;; | |
esac | |
} | |
function create_incremental() { | |
echo "++ create_incremental() BEGIN" | |
check_incremental_file incremental_file | |
if [ $? -eq $INC_NEW ]; then | |
create_full | |
return | |
fi | |
cd $WWW_DIR | |
$TAR cvpjf $BACKUP_DIR/$APP_FS_INC_FILE -g $incremental_file ./$INSTANCIA 2>&1 | |
echo "-- create_incremental() END" | |
} | |
function create_full() { | |
echo "++ create_full() BEGIN" | |
check_incremental_file incremental_file | |
if [ $? -eq $INC_EXISTS ]; then | |
mv $incremental_file ${incremental_file%"current"}$($DATE -I).finished | |
check_incremental_file incremental_file | |
fi | |
cd $WWW_DIR | |
$TAR cvpjf $BACKUP_DIR/$APP_FS_FULL_FILE -g $incremental_file ./$INSTANCIA 2>&1 | |
echo "-- create_full() END" | |
} | |
function create_db_backup() { | |
echo "++ create_db_backup() BEGIN" | |
$MYSQLDUMP --opt --host=$APP_HOST_DB --user=$APP_USER_DB --password=$APP_PASSWD_DB $APP_NAME_DB > $BACKUP_DIR/$APP_DB_FILE | |
$GZIP -f $BACKUP_DIR/$APP_DB_FILE | |
echo "-- create_db_backup() END" | |
} | |
function delete_expired() { | |
echo "++ delete_expired() BEGIN" | |
if [ $EXPIRE_TIME -gt 0 ]; then | |
$FIND $BACKUP_DIR -maxdepth 1 -mtime +$EXPIRE_TIME -exec rm -f '{}' \; | |
fi | |
echo "-- delete_expired() END" | |
} | |
### ------------------------------------------------------------ | |
### Main Script. | |
### ------------------------------------------------------------ | |
## Syntax check | |
if [ $# -lt 1 ]; then | |
echo "$(basename $0) <instancia>" | |
echo "** ERROR: Falta un parametro." | |
exit 1 | |
fi | |
## APP directory check | |
if [ ! -d $WWW_DIR/$INSTANCIA ]; then | |
echo "** ERROR: No existe el directorio de la instancia [$INSTANCIA]." | |
exit 2 | |
fi | |
## Backup directory check | |
if [ ! -d $BACKUP_DIR ]; then | |
echo -n "** No existe el directorio destino..." | |
$MKDIR -p $BACKUP_DIR | |
echo " created [$BACKUP_DIR]" | |
fi | |
## Day of week check | |
DAY_OF_WEEK=$($DATE +%u) | |
## Daily tasks | |
delete_expired | |
## Weekly tasks | |
case $DAY_OF_WEEK in | |
"1") | |
create_incremental | |
;; | |
"2") | |
create_incremental | |
;; | |
"3") | |
create_incremental | |
;; | |
"4") | |
create_incremental | |
;; | |
"5") | |
create_incremental | |
;; | |
"6") | |
create_full | |
;; | |
"7") | |
;; | |
*) | |
echo "* ERROR: DAY_OF_WEEK=($DAY_OF_WEEK) valor incorrecto." | |
exit 4 | |
;; | |
esac | |
create_db_backup | |
echo ":: Backup de instancia ($INSTANCIA) el dia $TODAY en $BACKUP_DIR" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment