diff --git a/storage/backup_database/backup_db.sh b/storage/backup_database/backup_db.sh new file mode 100644 index 0000000..550bf53 --- /dev/null +++ b/storage/backup_database/backup_db.sh @@ -0,0 +1,39 @@ +#!/bin/bash +#---------------------------------------- +# MySQL Backup Script +#---------------------------------------- + +# 🔧 Configuration +HOST='localhost' # MySQL server host +USER='root' # Username +PASSWORD='salam' # Password +DB='nutzwerte_fi4' +DAYS_TO_KEEP=60 # Delete backups older than X days +GZIP=1 # 1 = gzip compression, 0 = plain .sql +BACKUP_PATH='/var/www/html/nutzwerte_fi4/storage/backup_database/archive' # Location to store backups +#TIMESTAMP=$(date +"%d_%m_%Y_%H_%M_%S") # Date-time for unique file names +TIMESTAMP=$(date -d "yesterday" +"%d_%m_%Y") #Date-time for previous date file names. reminder here crontab use after 12 am + +#---------------------------------------- + +# Create the backup folder +if [ ! -d $BACKUP_PATH ]; then + mkdir -p $BACKUP_PATH +fi + + + # Backup (with or without compression) + if [ "$GZIP" -eq 0 ]; then + echo "Backing up database: $DB (no compression)" + mysqldump -h "$HOST" -u "$USER" -p"$PASSWORD" --databases "$DB" --single-transaction --quick --extended-insert --compact --hex-blob --max-allowed-packet=256M --net-buffer-length=2M > "$BACKUP_PATH/${DB}-${TIMESTAMP}.sql" + else + echo "Backing up database: $DB (with compression)" + mysqldump -h "$HOST" -u "$USER" -p"$PASSWORD" --databases "$DB" --single-transaction --quick --extended-insert --compact --hex-blob --max-allowed-packet=256M --net-buffer-length=2M | gzip -9 -c > "$BACKUP_PATH/${DB}-${TIMESTAMP}.sql.gz" + fi + + +# Delete old backups +if [ "$DAYS_TO_KEEP" -gt 0 ] ; then + echo "Deleting backups older than $DAYS_TO_KEEP days" + find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \; +fi