今天大样做了个简单的数据库备份脚本
#!/bin/bash # Database info bakupmysql TANK 2009/11/04 DB_USER="user" #用户名 DB_PASS="password" #密码 DATE=`date '+%Y%m%d-%H%M'` #要备份的日期 BIN_DIR="/usr/bin" BCK_DIR="/usr/local/database/database_bak/$DATE" #备份路径 LogFile="/usr/local/database/data_backup.log" #日志路劲 if [ ! -d $BCK_DIR ]; then mkdir -p "$BCK_DIR" fi echo "=================================== $DATE backup"===============================>>$LogFile cd $BCK_DIR #需要备份的库DB_NAMES=("db1" "db2") for dbName in ${DB_NAMES[@]}; do echo "begin backup $dbName...">>$LogFile #备份 ${BIN_DIR}/mysqldump --opt -h 192.168.1.104 -u${DB_USER} -p${DB_PASS} $dbName > $BCK_DIR/$dbName.sql if [[ $?==0 ]]; then echo "backup success!">>$LogFile else echo "backup Fail! Fail Fail Fail Fail ">>$LogFile fi echo "">>$LogFile done
当时遇到一个问题:mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using
password: YES) when trying to connect
解决方法:
grant all privileges on *.* to root@'localhost' identified by 'password';
我这里用的是ip地址,如果是本地可以用localhost。
为脚本添加可执行权限: chmod +x /usr/sbin/DataBackup
如果需要定时执行备份命令的,只需将下面这段代码放到crontab 文件(sudo vim /etc/crontab)中去就可以了:
01 1 * * * root /usr/sbin/MysqlDataBackup #它代表着将于每天1点执行DataBackup脚本
来源:https://www.cnblogs.com/qwj888/archive/2012/09/14/mysql.html