info@techdevops.com | 437-991-3573 | Data Engineering Services
TechDevOps.com
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance



Linux & MySQL Automated Database Backups
by BF (Principal Consultant; Architecture; Engineering)
2015-06-28







Solution:


Script:

#!/bin/sh

mkdir /DBbackups/$(date +%Y%m%d)

mysqldump -uuser -ppassword --opt DBNAMEHERE > /DBbackups/$(date +%Y%m%d)/dbnamehere_$(date +%Y%m%d).sql

cd /DBbackups/$(date +%Y%m%d)/
tar -zcvf filenamehere_$(date +%Y%m%d).tgz *.sql

cd /DBbackups
find . -type d -mtime +1 -exec rm -rf {} \;



Details:

#!/bin/sh
- SymLink to the currently configured System Shell - on Linux is usually either bash or dash. (i.e. Command Interpreter)

$
- means variable ($ will be replaced with what follows it)

tar
- The GNU version of the tar archiving utility

-zcvf
- Tar Options
- z means gzip
- c means create directory
- v means verbose
- f means file

.tgz *.sql
- Means take all .sql files and archive into one .tgz file

find . -type d -mtime +1 -exec rm -rf {} \;
. means in local
-type d means directory (f would mean files)
-mtime +1 means modified time - greater that 1 day
-exec rm -rf {} \; means execute the rm command(remove) with -rf option(remove directory and contents)(f means force - no prompt or message) {} \; means for each directory folder