文本文件存放在服务器a上,且文本文件的命名方式为yyyymmddhh,每小时导入24小时前生成的文件,而mysql安装在b上
首先在a上,从现有的文本文件截取所需的列(使用cut命令),然后传输到b。
b这台服务器既不能连接ftp服务器,也不能与其他机器配置用户等价性,因此向它传输文件只能使用scp,为了避免人机交互输入密码,采用了autoexpect命令
[root@justin]# autoexpect scp /var/www/data/info.txt root@b:/var/lib/mysql/market
autoexpect started, file is script.exp
root@b's password:
生成script.exp
[root@justin]# more generate_mysql_info.sh
#!/bin/bash
folder=/var/www/data/
#get the past time value
date1=`date --date='24 hour ago' +%Y%m%d%H`
cat "$folder""$date1" | cut -d '"' -f 19,20,21,31,32,33,34,35,36,37,41,42,43,61,62,63,64 | cut -d ',' -f 2,4,5,6,8,10 > info.txt
cd $folder
./script.exp
[root@justin]# crontab -l | grep generate
#generate and scp the info.txt to b for testing
30 * * * * sh /var/www/data/generate_mysql_info.sh >> /var/www/data/generate_mysql_info.log 2>&1
传输到64之后,然后调用mysqlimport导入mysql
字段中包含中文,数据库采用utf8字符集,为避免乱码,mysql建表语句后面加上 DEFAULT CHARSET=utf8
[root@db market]# more import_into_mysql.sh
#!/bin/bash
cd /var/lib/mysql/market
mysqlimport --user=root --password=******* --fields-enclosed-by='"' --fields-terminated-by=',' --default-character-set='utf8' market info.txt
[root@db market]# crontab -l
#import the info.txt into market.info
50 * * * * sh /var/lib/mysql/market/import_into_mysql.sh >> /var/lib/mysql/market/import_into_mysql.log 2>&1
至此job完成
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15480802/viewspace-694353/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15480802/viewspace-694353/