salt-states/restic/files/mysql-backup.sh

17 lines
832 B
Bash
Raw Normal View History

2023-06-27 07:16:37 +00:00
#!/bin/bash
umask 0077
BACKUP_DIR={{ pillar.containers.mariadb.backup_dir }}
databases=$(podman exec -it mariadb mysql -B -u root -p{{ pillar.containers.mariadb.env.MYSQL_ROOT_PASSWORD }} -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in ${databases[@]}; do
db=${db::-1}
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] && [[ "$db" != "sys" ]]; then
echo "Dumping database: $db"
podman exec -it mariadb mysqldump -u root -p{{ pillar.containers.mariadb.env.MYSQL_ROOT_PASSWORD }} --databases $db | gzip > ${BACKUP_DIR}/$(date +"%Y-%m-%d_%H-%M-%S")_$db-sql.gz
fi
done
# Delete the files older than 3 days
echo "removing old mysql dumps"
find $BACKUP_DIR/* -type f -name *-sql.gz -mtime +3 -exec rm {} \;
umask 0022