27-08-2025

This commit is contained in:
2025-08-27 09:14:20 +02:00
parent 947698f00b
commit 839d05bfbb
5 changed files with 122 additions and 66 deletions

View File

@@ -22,10 +22,33 @@
#
#
# 04-09-2024 : backup, sending remote, notification OK
# 04-09-2024 : TO DO : test deleting local files after $BKP_DAYS
red="\033[1;31m"
greenbold="\033[1;32m"
green="\033[0;32m"
yellow="\033[0;33m"
yellowbold="\033[1;33m"
bold="\033[1m"
#bold_under="\033[1;4m"
underline="\033[4m"
reset="\033[0m"
dotenv () {
set -a
# shellcheck disable=SC1091
[ -f "$HOME/.env" ] && . "$HOME/.env" || echo -e "${red}\nNo .env file found ! Could'nt get update from Github.'.${reset}"
set +a
}
dotenv
#CREATE USER 'mysqlbackupuser'@'localhost' IDENTIFIED BY '34diK=[6]Zui';
#GRANT SELECT ON * . * TO 'mysqlbackupuser'@'localhost';
pass=$(gpg --quiet --decrypt $HOME/.my.cnf.gpg)
pass=$(gpg --quiet --decrypt $HOME/.my.cnf.gpg)
x=$(echo $pass | awk '{print $2}')
BKP_USER=${x:5}
@@ -47,7 +70,7 @@ if [ ! -d $BKP_DEST ]; then mkdir $BKP_DEST; fi
## Note: Scripts will delete all backup which are older then BKP_DAYS##
#
# D:3 W:22 M:93
BKP_DAYS=3 # Enter how many days backup you want,
BKP_DAYS=1 # 3 Enter how many days backup you want,
BKP_WEEKS=21 # 3*7
BKP_MONTHS=93 # 3*31
#
@@ -82,33 +105,39 @@ IGNORE_DB="information_schema mysql performance_schema sys"
[ ! -d $BKP_DEST ] && mkdir -p $BKP_DEST || :
#
################# Autodetect the linux bin path #########################
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
#
notification() {
if [ $3 -eq 0 ];
then
sound="Boop"
message="Envoi terminé sur $2 !"
image="$BKP_BASE_DIR/success.png"
else
sound="Galet"
message="Echec lors de l'envoi sur $2 : erreur $result"
image="$BKP_BASE_DIR/error.png"
fi
if [[ "$OSTYPE" == "darwin"* ]] && [ -x "$(command -v terminal-notifier)" ]; then
terminal-notifier -title "$1" -message "$message" -sound "$sound" -contentImage "$image"
fi
curl -Is https://www.apple.com | head -1 | grep 200 1>/dev/null
if [[ $? -eq 1 ]]; then
echo -e "\n${red}No Internet connection !${reset}"
echo -e "No pushover notification !"
#exit 1
fi
send_pushover_notification() {
echo -e "Sending Pushover notification ..."
curl -s -F "token=$AIRBOOK_APP" \
-F "user=$USER_KEY" \
-F "title=$1" \
-F priority=2 \
-F html=1 \
-F retry=300 \
-F expire=86400 \
-F "message=$2" https://api.pushover.net/1/messages.json
[ $? -eq 0 ] && echo -e "${greenbold}\Pushover notification sent successfully !${reset}" || echo -e "${redbold}error sending Pushover notification !${reset}"
}
###################### Get database list ################################
#
DB_LIST="$($MYSQL -u $BKP_USER -h $MYSQL_HOST -p$BKP_PASS -Bse 'SHOW DATABASES')"
#DB_LIST="$($MYSQL -h $MYSQL_HOST -Bse 'SHOW DATABASES')"
: <<'END_COMMENT'
END_COMMENT
#
for db in $DB_LIST
@@ -131,33 +160,38 @@ do
# Dans les dossiers Backup: mkdir -p silverbook/Bases_MySQL
echo -e "Backup $db database..."
$MYSQLDUMP -u $BKP_USER -h $MYSQL_HOST -p$BKP_PASS -r$BKP_FILENAME $db
#$MYSQLDUMP -h $MYSQL_HOST -r$BKP_FILENAME $db
#echo $db
#cat $BKP_FILENAME
$GZIP -9 $BKP_FILENAME
f=$(basename "$BKP_GZ_FILENAME")
server1="ftp.cluster011.ovh.net:backup/$machine/Bases_MySQL/"
echo -e "Transfer $f to $server1"
scp "$BKP_GZ_FILENAME" funnymac@"$server1"
result=$?
result1=$?
notification "Backup MySQL: base $db" "$server1" $result
[ $? -ne 0 ] && send_pushover_notification "Backup MySQL Db" "Error when transfering $f on $s1 "
sleep 1
# NAS Synology: scp -O
server2="photos-nas.ovh:/volume1/Backup/$machine/Bases_MySQL/"
#scp -P42666 -p "$BKP_GZ_FILENAME" bruno@"$server2"
scp -P42667 -p "$BKP_GZ_FILENAME" bruno@"$server2"
#scp -P42666 -p "$BKP_GZ_FILENAME" bruno@clicclac.synology.me:/volume1/Backup/$machine/Bases_MySQL/
result=$?
s2=$(echo $server2 | awk -F":" '{print $1}')
echo -e "Transfer $f to $server2"
scp -O -P42667 -p "$BKP_GZ_FILENAME" bruno@"$server2"
result2=$?
notification "Backup MySQL: base $db" "$server2" $result
[ $? -ne 0 ] && send_pushover_notification "Backup MySQL Db" "Error when transfering $f on $s2 "
#echo "----"
echo
fi
done
: <<'END_COMMENT'
#########To delete all backup files older then BKP_DAYS #################
#
@@ -166,26 +200,35 @@ done
if find --version >/dev/null 2>&1 ; then
echo Using GNU date
d_duration="$BKP_DAYS"
w_duration="$BKP_DAYS"
m_duration="$BKP_DAYS"
w_duration="$BKP_WEEKS"
m_duration="$BKP_MONTHS"
else
echo Using BSD date
d_duration="$BKP_DAYS"d
w_duration="$BKP_DAYS"d
m_duration="$BKP_DAYS"d
w_duration="$BKP_WEEKS"d
m_duration="$BKP_MONTHS"d
echo "$d_duration"
echo "$w_duration"
echo "$m_duration"
fi
echo -e "Find daily backups..."
#find $BKP_DEST -name "*.sql.gz" -mtime +$BKP_DAYS | grep -v -E '(January|February|March|April|May|June|July|August|September|October|November|December)|(S\d{2}_\d{4})' | xargs rm -f
find $BKP_DEST -name "*.sql.gz" -mtime +"$d_duration" | grep -v -E '(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)|(S\d{2}_\d{4})' | xargs rm -f
find $BKP_DEST -name "*.sql.gz" -mtime +"$d_duration" | grep -v -E '(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)|(Sd{2}_d{4})' | xargs rm -f
##gfind /Users/bruno/Documents/MySQL -mtime +93 -iregex '.*\(January\|February\|March\|April\|May\|June\|July\|September\|October\|November\|December\).*'
echo "----"
find $BKP_DEST -name "*.sql.gz" -mtime +"$w_duration" | grep -E 'S\d{2}_\d{4}' | xargs rm -f
echo -e "Find weekly backups..."
find $BKP_DEST -name "*.sql.gz" -mtime +"$w_duration" | grep -E 'Sd{2}_d{4}' | xargs rm -f
##gfind /Users/bruno/Documents/MySQL -mtime +22 -regextype posix-extended -iregex '.*S[0-9]{2}_[0-9]{4}.*'
echo "----"
echo -e "Find monthly backups..."
#find $BKP_DEST -name "*.sql.gz" -mtime +$BKP_MONTHS | grep -E '(January|February|March|April|May|June|July|August|September|October|November|December)' | xargs rm -f
find $BKP_DEST -name "*.sql.gz" -mtime +"$m_duration" | grep -E '(janvier|février|mars|avril|mai|juin|juillet|août|septembre|octobre|novembre|decembre)' | xargs rm -f
#
: <<'END_COMMENT2'
# Nettoyage serveurs distants:
#916_dest="/volume1/Backup/SilverBook/Bases_MySQL"
@@ -242,6 +285,6 @@ done
#### End of script ####
END_COMMENT
END_COMMENT2