shell脚本定时备份数据

shell脚本编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

# Database info
DB_USER="" # username
DB_PASS="" # password
DB_HOST="" # host
DB_NAME="" #database name
DB_PORT=3309 # database port

# Others vars
BIN_DIR="/usr/local/mysql/bin" #mysql shell file
BCK_DIR="/opt/mysqlbackup/db" # mysql backup dir
DATE="`date +%Y-%m-%d-%H-%M-%S`"

# TODO
$BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS -h$DB_HOST -P$DB_PORT --skip-lock-tables $DB_NAME > $BCK_DIR/"$DB_NAME"_db_$DATE.sql

定时执行任务

1
2
# per 20 minutes execute
*/20 * * * * sh /opt/shell/db.sh