>데이터 베이스 >MySQL 튜토리얼 >Linux系统下mysql用shell脚本做备份_MySQL

Linux系统下mysql用shell脚本做备份_MySQL

WBOY
WBOY원래의
2016-06-01 13:10:44992검색

对于linux系统下的mysql DB的数据备份,以下两种方法,第一种方法是只备份数据,将数据保存在文本文件中;第二种方法是将整个DB备份,生成SQL文件,在DB恢复时较方便,当然,如果需要,也可以对个别表进行备份。

方法一:

#!/bin/bash#DB服务器IPDB_HOST="127.0.0.1"#database nameDB_NAME="mysql"#database usernameDB_USER="USER"#database passwordDB_PASS="password"#需要备份的表名TBL_NAME_1=user#需要备份的表名TBL_NAME_2=db#当前日期时间字符串 例:2010-12-20-10-12-30  (年月日时分秒)DATE=`date +%Y-%m-%d-%H-%M-%S`#执行mysql命令的参数SQL_OPT="-u$DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME"/usr/bin/mysql $SQL_OPT << EOF#将单个表的数据导出到文件中,#FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n' 这些选项是把数据用逗号分隔,双引号引起来,换行用/n;如果不用,可以将这些选项删除。select * from $TBL_NAME_1 into outfile "/tmp/bakfile/$TBL_NAME_1.$DATE.txt" FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';select * from $TBL_NAME_2 into outfile "/tmp/bakfile/$TBL_NAME_2.$DATE.txt" FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';quitEOFecho 'Backup success'

方法二:

#!/bin/bash#DB服务器IPDB_HOST="127.0.0.1"#database nameDB_NAME="mysql"#database usernameDB_USER="USER"#database passwordDB_PASS="password"#当前日期时间字符串 例:2010-12-20-10-12-30  (年月日时分秒)DATE=`date +%Y-%m-%d-%H-%M-%S`#bakup file absolute pathBAKUP_FILE=/tmp/bakfile/$DB_NAME-$DATE.sql#执行mysqldump命令的参数SQL_OPT="-u$DB_USER -p$DB_PASS -h $DB_HOST $DB_NAME"#执行备份,整个DB所有表的备份,SQL语句的形式/usr/bin/mysqldump --opt $SQL_OPT > $BAKUP_FILEecho 'Backup success。'


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.