The following articles mainly introduce the simple backup of MySQL databases, including how to configure the corresponding backup directory code on the server and the actual operation steps of the backup script, and how to use crontab to regularly execute the backup script. Hope you will have some gains.
Simple backup MySQL database 1: configure the Backup Directory code on the server:
- mkdir /var/lib/MySQLbackup
- cd /var/lib/MySQLbackup
Simple backup MySQL database 2: backup script:
- vi dbbackup.sh
Note: Change username, password, and dbname.
Enter the code:
- #!/bin/sh
- #Username to access the MySQL server
- USERNAME="username"
- # Username to access the MySQL server
- PASSWORD="password"
- # List of DBNAMES for Backup
- DBNAME="dbname"
- #date timestamp for log message
- DATE=`/bin/date +%Y-%m-%d_%Hh%Mm`
- #output file
- OUTDIR="/var/lib/MySQLbackup/"
- OUTFILE="ip_bindass".$DATE."sql.gz"
- #working directory
- DIR="/var/lib/MySQLbackup/"
- #cd $DIR
- # MySQL Backup
- /usr/bin/MySQLdump --database $DBNAME --opt --single-transaction
- -u$USERNAME -p$PASSWORD | /usr/bin/gzip -9 > $OUTDIR$OUTFILE
Change backup script permission
- chmod +x dbbackup.sh
Simple backup MySQL database 3: Use crontab to regularly execute the backup script:
- crontab -e
If you back up data at three o'clock P.M. every day, add the following code:
- 20 15 * * * /var/lib/MySQLbackup/dbbackup.sh
The above content is an introduction to the simple backup of the MySQL database. I hope you will have some gains.