BlueXIII's Blog

热爱技术,持续学习

0%

Oracle+MySQL+InfluxDB简易备份脚本

Oracle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# ##################################################################
# Oracle定时Expdp脚本
# SQL> CREATE DIRECTORY backup as '/home/oracle/dump';
# SQL> grant read,write on directory backup to SYSTEM;
# crontab -e
# 30 00 * * * . $HOME/.bash_profile; cd /home/oracle/sh; ./bak_exp.sh
# ##################################################################

# 环境变量
USERNAME=system
PASSWORD=yourpass
SID=orcl
SCHEMA=yoursid
DUMP_DIR=/home/oracle/dump
DIRECTORY=BAKDIR
PARALLEL=1
DMP_FILE=BAK_`date +%Y%m%d%H%M%S`.dmp
LOG_FILE=BAK_`date +%Y%m%d%H%M%S`.log

# expdp导出
expdp $USERNAME@$SID/$PASSWORD schemas=$SCHEMA DIRECTORY=$DIRECTORY DUMPFILE=$DMP_FILE logfile=$LOG_FILE parallel=$PARALLEL;

# 压缩dmp
cd $DUMP_DIR
gzip $DMP_FILE

# 删除旧的dmp
find $backup_dir -name "*.dmp" -mtime +1 -exec rm {} \;

MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# ##################################################################
# MySQL定时备份
# crontab -e
# 30 01 * * * . $HOME/.bash_profile; cd /home/youruser/mysqlbak/sh; ./bak_mysql.sh
# ##################################################################

# 环境变量
USERNAME=root
PASSWORD=yourpass
DATABASE=yourdb
BASE_DIR=/home/youruser/mysqlbak
DUMP_FILE=$BASE_DIR/BAK_`date +%Y%m%d%H%M%S`.sql

# 备份
cd $BASE_DIR
mysqldump -u$USERNAME -p$PASSWORD $DATABASE > $DUMP_FILE

# 压缩dmp
gzip $DUMP_FILE

# 删除旧的dmp
find $BASE_DIR -name "*.sql.gz" -mtime +7 -exec rm {} \;

InfluxDB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
# ##################################################################
# InfluxDB定时备份
# crontab -e
# 30 00 * * * . $HOME/.bash_profile; cd /home/youruser/influxbak/sh; ./bak_influx.sh
# ##################################################################

# 环境变量
DATABASE=yourdb
BASE_DIR=/home/youruser/influxbak
DUMP_DIR=$BASE_DIR/BAK_`date +%Y%m%d%H%M%S`

# 备份
mkdir $DUMP_DIR
influxd backup -portable -database $DATABASE $DUMP_DIR

# 删除旧的dmp
cd $BASE_DIR
find $BASE_DIR -type d -name "BAK_*" -mtime +7 -exec rm -rf {} \;