#!/bin/bash if [ $# != 1 ]; then echo Usage: ./sync_crp.sh arg1 echo Examples: echo To get the date of 2011-09-01, enter: echo ' sync_crp.sh 2011-09-01 > 2011-09-01.out' exit 0 fi export PGPORT=1921 export PGDATA=/database/pgdata1921 export LANG=en_US.utf8 export PGHOME=/app/pgsql export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib export DATE=`date +"%Y%m%d%H%M"` export PATH=$PGHOME/bin:$PATH:. export MANPATH=$PGHOME/man:$MANPATH today=$1 nextday=`date -d "${today} +1 days" +%F` host_ip='192.168.xx.xx' v_email="[Francs3@163.com](mailto:Francs3@163.com)" #Make SQL strings, the sql of getting database source_db data sql1="select * from skytf.hk_tbl_charge_oversea where createtime >= '${today}' and createtime <'${nextday}' " sql2="select * from skytf.hk_tbl_charge_oversea_dsm where createtime >= '${today}' and createtime <'${nextday}' " ################## The real get data process ###################### ## 同步表 crp.hk_tbl_charge_oversea echo "`date +%F %T` Begin sync report data from data source_db to crp database ! " psql -h 192.168.xx.xx -p 1921 source_db francs -c "copy ($sql1) to stdout " | psql des_db des_db -c "copy crp.hk_tbl_charge_oversea from stdin" if [ $? -ne 0 ] then echo "`date +%F %T` ERROR: Sync table hk_tbl_charge_oversea error " echo -e "`date +%F %T`nIP: ${host_ip}nHostname: `hostname`nnAuthor: francs(DBA)" | mutt -s " ERROR: Sync table hk_tbl_charge_oversea error " ${v_email} else echo "`date +%F %T` Table hk_tbl_charge_oversea is finished " fi ## 同步表 crp.hk_tbl_charge_oversea_dsm psql -h 192.168.xx.xx -p 1921 source_db francs -c "copy ($sql2) to stdout " | psql des_db des_db -c "copy crp.hk_tbl_charge_oversea_dsm from stdin" if [ $? -ne 0 ] then echo "`date +%F %T` ERROR: Sync table hk_tbl_charge_oversea_dsm error " echo -e "`date +%F %T`nIP: ${host_ip}nHostname: `hostname`nnAuthor: francs(DBA)" | mutt -s " ERROR: Sync table hk_tbl_charge_oversea_dsm error " ${v_email} else echo "`date +%F %T` Table hk_tbl_charge_oversea_dsm is finished " fi
备注:脚本里只同步了两张表,如果还有其它表,可以继续加代码,如果表名一样,可以写个 for 循环,由于这里源表名,目标表名不一样,所以没用 for 循环, 这个脚本按天同步数据,如果需要补数据,只要加上日期参数即可;
设置任务计划
加入 crontab 定时执行,如下
1 2
# Sync crp data from data warehouse 6* * * * /home/postgres/script/tf/sync_script/exec_sync_crp.sh >> /home/postgres/script/tf/sync_script/exec_sync_crp.log 2>&1