mirror of
https://github.com/fradelg/docker-mysql-cron-backup.git
synced 2025-01-18 16:22:33 +01:00
Merge pull request #18 from mmartinortiz/fix-symlink
Fix symlink && increased verbosity
This commit is contained in:
commit
6fdc59d957
1 changed files with 8 additions and 6 deletions
14
backup.sh
14
backup.sh
|
@ -3,20 +3,21 @@
|
||||||
[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
||||||
|
|
||||||
DATE=$(date +%Y%m%d%H%M)
|
DATE=$(date +%Y%m%d%H%M)
|
||||||
echo "=> Backup started at $DATE"
|
echo "=> Backup started at $(date "+%Y-%m-%d %H:%M:%S")"
|
||||||
databases=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}}
|
databases=${MYSQL_DATABASE:-${MYSQL_DB:-$(mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)}}
|
||||||
for db in $databases
|
for db in $databases
|
||||||
do
|
do
|
||||||
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
|
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]]
|
||||||
then
|
then
|
||||||
echo "Dumping database: $db"
|
echo "==> Dumping database: $db"
|
||||||
FILENAME=/backup/$DATE.$db.sql
|
FILENAME=/backup/$DATE.$db.sql
|
||||||
LATEST=/backup/latest.$db.sql.gz
|
LATEST=/backup/latest.$db.sql.gz
|
||||||
if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" --databases "$db" "$MYSQLDUMP_OPTS" > "$FILENAME"
|
if mysqldump -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" --databases "$db" $MYSQLDUMP_OPTS > "$FILENAME"
|
||||||
then
|
then
|
||||||
gzip -f "$FILENAME"
|
gzip -f "$FILENAME"
|
||||||
|
echo "==> Creating symlink to latest backup: $(basename "$FILENAME".gz)"
|
||||||
rm "$LATEST" 2> /dev/null
|
rm "$LATEST" 2> /dev/null
|
||||||
ln -s "$FILENAME" "$LATEST"
|
cd backup && ln -s $(basename "$FILENAME".gz) $(basename "$LATEST") && cd ..
|
||||||
else
|
else
|
||||||
rm -rf "$FILENAME"
|
rm -rf "$FILENAME"
|
||||||
fi
|
fi
|
||||||
|
@ -25,12 +26,13 @@ done
|
||||||
|
|
||||||
if [ -n "$MAX_BACKUPS" ]
|
if [ -n "$MAX_BACKUPS" ]
|
||||||
then
|
then
|
||||||
|
echo "=> Max number of backups ("$MAX_BACKUPS") reached. Deleting oldest backups"
|
||||||
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
|
while [ "$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | wc -l)" -gt "$MAX_BACKUPS" ]
|
||||||
do
|
do
|
||||||
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | sort | head -n 1)
|
TARGET=$(find /backup -maxdepth 1 -name "*.sql.gz" -type f | sort | head -n 1)
|
||||||
echo "Backup $TARGET is deleted"
|
|
||||||
rm -rf "$TARGET"
|
rm -rf "$TARGET"
|
||||||
|
echo "==> Backup $TARGET has been deleted"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "=> Backup done"
|
echo "=> Backup process finished at echo $(date "+%Y-%m-%d %H:%M:%S")"
|
||||||
|
|
Loading…
Reference in a new issue