linux环境mysql导出数据并上传sftp脚本

#define mysql info
#mysql数据库的地址
_db_host='-h192.168.1.1'
#mysql的端口
_db_port='-p3306'
#mysql的登陆账号
_db_user='-uroot'
#mysql的密码
_db_pass='-p1qazxsw2'
#mysqlde db名字
_db_database='-Dsampling'

#导出数据的路径
_file_path='/opt/mysql/data/'

#远程sftp的路径
_remote_path='/IN/test/'
#sftp host
_sftp_host='sftp.baidu.com.cn'
#sftp的账号
_sftp_user='g091'
#sftp的密码
_sftp_pass='Vp1vcGs'


#导出文件的路径
_file_region="${_file_path}region_$(date +%Y%m%d).csv"
_file_customermessage="${_file_path}customermessage_$(date +%Y%m%d).csv"
_file_order="${_file_path}order_$(date +%Y%m%d).csv"
_file_order_item="${_file_path}order_item_$(date +%Y%m%d).csv"
_file_t_activity="${_file_path}t_activity_$(date +%Y%m%d).csv"
_file_t_activity_pack="${_file_path}t_activity_pack_$(date +%Y%m%d).csv"
_file_t_goods="${_file_path}t_goods_$(date +%Y%m%d).csv"
_file_t_goods_pack="${_file_path}t_goods_pack_$(date +%Y%m%d).csv"
_file_comments="${_file_path}comments_$(date +%Y%m%d).csv"
#删除历史文件
rm -rf ${_file_path}*

echo 'mysql info:'
echo ' host-> '${_db_host}
echo ' port-> '${_db_port}
echo ' user-> '${_db_user}
echo ' pass-> '${_db_pass}

echo 'Export Region'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `ID`, `NAME`, `PARENT_ID`, `PRIORITY`, `FEE`, `LEVEL`, `type`, `ispilot`, `goodstype` FROM `region`' >>${_file_region}

echo 'Export customermessage'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT c.`id`,c.`name`,c.`mobile`,c.`openId`,c.`occupation`,c.`province`,c.`city`,c.`area`,c.`address`,c.`restaurant`,c.`resType`,c.`supplier`,c.`supplierProvince`,c.`supplierCity`,c.`supplierArea`,c.`market`,c.`supplierMobile`,c.`supplierPhone`,c.`created`,c.`activityId`,c.`formType`,c.`brand`,c.`amount`,c.`KitchenPhotoUrl`,c.`consumption`,c.`user_from`,c.`status`,c.`type` FROM customermessage c WHERE DATE(c.`created`)=DATE(DATE_SUB(NOW(),INTERVAL 1 DAY))'>>${_file_customermessage}


echo 'Export order'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `order_id`, `order_number`, `user_id`, `consignee`, `province_id`, `city_id`, `area_id`,`province_name`, `city_name`, `area_name`, `address`, `post`, `telephone`, `email`, `product_money`, `total_money`,`freight_money`, `paytype`, `invoice_content`, `giftcode`, `create_date`, `order_status`, `brand_id`, `update_date`, `tradeNo`,`outTradeNo`, `reason`, `total_fee`, `tranDate`, `product_count`, `user_email`, `backorderid`, `memo`, `discount_money`, `auditStatus`,`waybillNum`, `receiveDate`, `autoStatus`, `fansTag`, `customerStatus`, `logistics`, `logisticsProviderCode`, `skuCode`, `codeType`, `auditTime`, `activityId`, `packid`, `auditer`, `isFans` FROM `order` WHERE DATE(create_date)=DATE(DATE_SUB(NOW(),INTERVAL 1 DAY))'>>${_file_order}

echo 'Export order item'
mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `item_id`, `product_id`, `order_id`, `buy_price`, `buy_count`, `cam_id`, `cam_name`, `create_date` FROM `order_item` WHERE DATE(create_date)=DATE(DATE_SUB(NOW(),INTERVAL 1 DAY));SELECT `item_id`, `product_id`, `order_id`, `buy_price`, `buy_count`, `cam_id`, `cam_name`, `create_date` FROM `order_item` WHERE DATE(create_date)=DATE(DATE_SUB(NOW(),INTERVAL 1 DAY))'>>${_file_order_item}


echo 'Export t_activity'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `id`, `name`, `startTime`, `endTime`, `canUseNum`, `addressUseNum`, `activityUrl`, `isCheck`, `isCalls`,`isQuestion`, `createTime`, `updateTime`, `isLive`, `isFans`, `pageTitle`,`isSupplier`, `isCoupon`, `wjPage`, `stockManage`, `totalCount` FROM `t_activity`'>>${_file_t_activity}

echo 'Export t_activity_pack'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `id`, `activityId`, `goodsId`, `whiteList`, `openWhiteList`, `blackList`, `openBlackList`, `pay`, `payMoney`, `unitPost`, `unitPostMoney`, `totalCount` FROM `t_activity_pack` t WHERE EXISTS (SELECT 1 FROM t_activity a WHERE a.`id` = t.`activityId`)'>>${_file_t_activity_pack}

echo 'Export t_goods'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `id`, `name`, `code`, `img`, `weight`, `type`, `price`, `description`, `createTime`, `updateTime`, `isLive` FROM `t_goods`'>>${_file_t_goods}


echo 'Export t_goods_pack '

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `id`, `goodsId` FROM `t_goods_pack` t WHERE EXISTS (SELECT 1 FROM t_goods g WHERE g.id = t.`id`)'>>${_file_t_goods_pack}


echo 'Export comments'

mysql ${_db_host} ${_dp_port} ${_db_user} ${_db_pass} ${_db_database} -e 'SELECT `id`, `skuCode`, `openId`, `mobile`, `content`, `score`, `intention`, `created`, `status`, `type`, `amount`, `alterType`, `resType`, `consumption`, `brand`, `activityId`, `parentId`FROM `comments`WHERE DATE(created)=DATE(DATE_SUB(NOW(),INTERVAL 1 DAY))' >>${_file_comments}


lftp -u ${_sftp_user},${_sftp_pass} sftp://${_sftp_host}< mirror -R ${_file_path} ${_remote_path}
EOF

你可能感兴趣的:(linux环境mysql导出数据并上传sftp脚本)