Backup mysql database regularly

write a script

 

#!/bin/bash
DB_NAME=$1
if [ "$DB_NAME"x == ""x ]; then
echo "you need input a db name." && exit 0
be
MYSQL_HOME=/usr/local/mysql
MYSQL_DUMP=$MYSQL_HOME/bin/mysqldump
BACKUP_DB_DIR=/data/backup/db/$DB_NAME
if [ ! -d  "$BACKUP_DB_DIR" ]; then
mkdir -p $ BACKUP_DB_DIR
be
TIME="$(date +"%Y%m%d%H%M")"
$MYSQL_HOME/bin/mysqldump -uusername -ppassword db_psq |gzip > $BACKUP_DB_DIR/$DB_NAME-$TIME.sql.gz
find $BACKUP_DB_DIR -name "$DB_NAME*.sql.gz" -type f -mtime +30 -exec rm {} \; > /dev/null 2>&1

 

 

find $BACKUP_DB_DIR -name "$DB_NAME*.sql.gz" -type f -mtime +30 -exec rm {} \; > /dev/null 2>&1

 

It means in the backup directory, check the files with the prefix of $DB_NAME and the suffix of sql.gz, where -type f represents ordinary files, mtime +30 represents files 30 days ago, and after finding the files, execute -exec rm {} to delete, /dev/null 2>&1 means that the standard error is output to the standard output and thrown to /dev/null, that is, it can be considered to be thrown into the trash can.

 

Put the shell script in the /usr/local/mysql/bin directory

Usage /usr/local/mysql/bin/mysql_backup.sh db_name

 

If you need scheduled backups, you can configure a cron scheduled task

crontab -e

 

30 3 * * * (/usr/local/mysql/bin/mysql_backup.sh db_name) > /dev/null 2>&1

 

It means to back up the database db_name at 3:30 in the morning every day. Of course, this database must exist in the database.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326701148&siteId=291194637