Home > Database > Mysql Tutorial > linux mysql备份数据库

linux mysql备份数据库

WBOY
Release: 2016-06-07 16:39:51
Original
992 people have browsed it

#!/bin/sh # author shiwei.du # date 2013-10-30 NOW_TIME=`date "+%y%m%d%H%M%S"` # 现在时间戳 OVER_TIME=`date -d'3 day ago' "+%y%m%d%H%M%S"` #过期的时间戳 DBHOST=192.168.1.21 #服务器ip地址 DBNAME=test #数据库名称 DBUSER=root #操作者 DBPASSWD

#!/bin/sh
# author shiwei.du
# date   2013-10-30

NOW_TIME=`date "+%y%m%d%H%M%S"` # 现在时间戳
OVER_TIME=`date -d'3 day ago' "+%y%m%d%H%M%S"`  #过期的时间戳
DBHOST=192.168.1.21 #服务器ip地址
DBNAME=test  #数据库名称
DBUSER=root #操作者
DBPASSWD=****** #密码
DBCHARACTER=utf8 #数据库编码
BACKUPPATH="/home/mysqlbackup/"   #备份路径
BACKUPLOG=$BACKUPPATH"mysqlbackup.log"  #备份日志文件

# 判断存放备份文件的目录是否存在,不存在创建并修改文件的权限
if [ ! -d "$BACKUPPATH" ]; then
   mkdir -p -m 700 "$BACKUPPATH"
fi

# 判断日志文件是否存在
if [ ! -f "$BACKUPLOG" ]; then
  touch "$BACKUPLOG"
fi

# 执行mysqldump命令导出将要备份的数据库
`mysqldump -h$DBHOST -u$DBUSER -p$DBPASSWD --default-character-set=$DBCHARACTER --opt $DBNAME | gzip > $BACKUPPATH$DBNAME$NOW_TIME.sql.gz`

#记录日志文件
echo "[CREATE NEW BACKUP FLLE $BACKUPPATH$DBNAME$NOW_TIME.sql.gz SUCCESS!]"[`date "+%y-%m-%d %H-%M"`] >> $BACKUPLOG

#删除过期的备份文件
rm -f $BACKUPPATH$DBNAME$OVER_TIME.sql.gz | echo "[REMOVE FILE $BACKUPPATH$DBNAME$OVER_TIME.sql.gz SUCCESS!]" >> $BACKUPLOG

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template