backup_mariadb.sh
#!/bin/bash
USER="root"
PASSWORD="..."
OUTPUT="/home/pi/shared/backup_mariadb"
# rm "$OUTPUTDIR/*gz" > /dev/null 2>&1
databases=`mysql -h 127.0.0.1 -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump -h 127.0.0.1 -u $USER -p$PASSWORD --databases $db > $OUTPUT/`date +%Y%m%d`.$db.sql
# gzip $OUTPUT/`date +%Y%m%d`.$db.sql
fi
done
Run ทุกๆ ตี 3 ด้วย crontab
crontab -e
0 3 * * * /usr/bin/bash /home/pi/backup_mariadb.sh >> /home/pi/shared/log_mariadb.txt