Regularly export data scripts from mongoDB on Linux

Source: Internet
Author: User

Purpose: compile a shell script to export data from mongoDb to a csv file. Requirements: 1. To avoid exporting massive amounts of data, you only need to filter out the data for the current month and the data for the previous month. 2. Because the data of the day is incomplete, We need to exclude the data of the day. The following is an example --------------------------------------------------------------------------#! /Bin/bash # create and save directory mypath = micAnalyDir/'date + % Y/% m % d' mkdir-p $ mypath
# Calculate the filter time baseDate = 'date -- date' 1 months ago '+' % m/01/% y' echo $ baseDatedeclare-I baseTimebaseTime = $ ('date -- date "$ baseDate" + "% s" '* 1000 ))
# Export the data function exportData (){ExportCmd = "/kingdee/mongodb/bin/export -- csv"If [$ #-gt 3]; then # note that baseTime and endTime cannot be enclosed in quotation marks; otherwise, an error is reported.$ ExportCmd-d $1-c $2-f $3-q \"{\" $4 \ ": {\" \ $ gte \ ": {\" \ $ date \ ": $ baseTime },\\ "\ $ Lt \": {\ "\ $ date \": $ endTime }}"-o $ mypath/export 2.csvElse$ ExportCmd-d $1-c $2-f $3-o $ mypath/export 2.csvFi}
# Export all data of the first table echo export data from $ baseDateecho 'export ossDev. t_User 'fields = "_ id, email, registerDate, photoId" exportData "ossDev" "T_User" $ fields
# Export the sendTime of the second table to echo 'export snsDev. t_Comment 'fields = "_ id, del, from, mbId, mbOwner, nwId, ownerId, sendTime" exportData "snsDev" "T_Comment" $ fields "sendTime" begin is saved. sh file, use chmod + x to set the script to executable, and then use the crontab command to add the script to the scheduled task.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.