Differences
This shows you the differences between two versions of the page.
— | database:mysql:backup [2019/10/31 09:04] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Bash script MySql Backup ====== | ||
+ | If you host any MySql databases, you should certianly make use of the command line tools to make backups. There are never enough backups. | ||
+ | |||
+ | The script below this text, in combination with the specified crontab entries, will give you the following backup: | ||
+ | * Last 24 hours - on the hour | ||
+ | * Last 7 days - at midnight | ||
+ | * Last 12 months - at midnight on the first of the month | ||
+ | * Every year - at midnight on the first of January | ||
+ | First, we create a mysql host defaults config file our script will use - eg: / | ||
+ | <code bash mysql_backup_localhost.cnf> | ||
+ | [client] | ||
+ | user = ro_usr | ||
+ | password = 2084mfsm!sdaDSA | ||
+ | host = localhost | ||
+ | </ | ||
+ | Note that the username and password is in clear text on a local disk, so it's good practice to create an user for backup. An user that has specific read only privileges, so nobody can modify your database with this user. On the other hand - it's not good practice to keep passwords in scripts at all. | ||
+ | |||
+ | Now, create a script called mysql_backup.sh in / | ||
+ | <code bash mysql_backup.sh> | ||
+ | #!/bin/bash | ||
+ | _MSG=" | ||
+ | # Check if config file is specified, if it exists and if it's readable | ||
+ | if [[ $1 == "" | ||
+ | _MSG=" | ||
+ | else | ||
+ | if [[ ! -e $1 ]] ; then | ||
+ | _MSG=" | ||
+ | else | ||
+ | if [[ ! -r $1 ]] ; then | ||
+ | _MSG=" | ||
+ | else | ||
+ | _MSG=" | ||
+ | fi | ||
+ | fi | ||
+ | fi | ||
+ | |||
+ | # Check if backup type id specified and generate filename depending on backup type | ||
+ | _dow=" | ||
+ | _dom=" | ||
+ | _moy=" | ||
+ | _hod=" | ||
+ | _yr=" | ||
+ | case " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | "" | ||
+ | ;; | ||
+ | |||
+ | *) _MSG=" | ||
+ | ;; | ||
+ | esac | ||
+ | |||
+ | # Check if path is specified and add a trailing / if needed | ||
+ | if [[ $3 != "" | ||
+ | tmp=$3 | ||
+ | _lc=" | ||
+ | _lc=" | ||
+ | if [[ $_lc != "/" | ||
+ | _od=" | ||
+ | else | ||
+ | _od=" | ||
+ | fi | ||
+ | else | ||
+ | _od=" | ||
+ | fi | ||
+ | _MSG=" | ||
+ | |||
+ | # Check if specified path exists | ||
+ | if [ ! -d " | ||
+ | _MSG=" | ||
+ | fi | ||
+ | |||
+ | # List all databases | ||
+ | databases=`mysql --defaults-extra-file=$1 --batch --skip-column-names -e "SHOW DATABASES;" | ||
+ | |||
+ | # Loop through databases and dump them | ||
+ | for db in $databases; do | ||
+ | if [[ " | ||
+ | _MSG=" | ||
+ | mysqldump --defaults-extra-file=$1 --databases $db --routines | gzip > " | ||
+ | fi | ||
+ | done | ||
+ | _MSG=" | ||
+ | if [[ $RUN_BY_CRON!=" | ||
+ | </ | ||
+ | The script first checks if the config file is specified, then if the file exists and finaly if it is readable. Then it checks if backup type is specified, and generates the filename accordingly. After that, it checks if backup path is specified and if it exists. If the backup path is not specified, the running directory will be used. Finaly, the script loops through databases on the specified server and dumps every database in a separate gziped file. | ||
+ | |||
+ | And finally, we create several crontab entrys so our script can run (Since I have a specified path for storing backups in mind, I'll specify it - note that trailing / is optional): | ||
+ | <code bash> | ||
+ | RUN_BY_CRON=" | ||
+ | 0 0 1 1 * / | ||
+ | 0 0 1 * * / | ||
+ | 0 0 * * * / | ||
+ | 0 * * * * / | ||
+ | </ | ||
+ | Please note that I use crontab to set the variable RUN_BY_CRON to " | ||
+ | |||
+ | Instead of the first five fields, you may use one of eight special strings - to increase readability: | ||
+ | <code bash> | ||
+ | @reboot - Run once, at startup. | ||
+ | @yearly | ||
+ | @annually - same as @yearly | ||
+ | @monthly | ||
+ | @weekly | ||
+ | @daily | ||
+ | @midnight - same as @daily | ||
+ | @hourly | ||
+ | </ | ||
+ | Check [[linux: | ||