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 large amounts of data, you only need to filter out the current month

Purpose: compile a shell script to export data from mongoDb to a csv file. Requirements: 1. To avoid exporting large amounts of data, you only need to filter out the current month

Purpose: compile a shell script to export data from mongoDb to a csv file.

Requirements:

1. To avoid exporting huge amounts of data, you only need to filter out the data of the current month and the data of the previous month.

2. Because the data of the current day is incomplete, You need to exclude the data of the current day.

The following is an example

--------------------------------------------------------------------------

#! /Bin/bash

# Create a 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 $ baseDate

Declare-I baseTime

BaseTime = $ ('date -- date "$ baseDate" + "% s" '* 1000 ))


# Exporting data from a specified data table

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.csv

Else

$ ExportCmd-d $1-c $2-f $3-o $ mypath/export 2.csv

Fi

}


# Export all data in the first table

Echo export data from $ baseDate

Echo 'export ossDev. t_user'

Fields = "_ id, email, registerDate, photoId"

ExportData "ossDev" "T_User" $ fields


# Export the sendTime of the second table as the data for the current month and the previous month

Echo 'export snsDev. t_comment'

Fields = "_ id, del, from, mbId, mbOwner, nwId, ownerId, sendTime"

ExportData "snsDev" "T_Comment" $ fields "sendTime"

----------------------------------------------------------------------------

Save it as a. 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.