Mysql定时备份shell

Standard
#!/bin/sh
dbname=yemaosheng_com #数据库名称
backupDir=/tmp/mysqlbackup/ #备份存放路径
cd $backupDir
if [ -f $dbname".tar.gz" ]
then
        rm -rf $dbname
        rm -rf $dbname".tar.gz"
fi
mysqlhotcopy $dbname $backupDir
tar zcvf $dbname".tar.gz" "./"$dbname
#! /bin/bash
 
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
 
mkdir -p "$BACKUP_DIR/mysql"
 
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
 
for db in $databases; do
  $MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done

保存到 /etc/cron.daily/ 目录下,加上777就行

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.