linux shell 导出mongodb 数据库 & 导出 mysql数据库

导出mongodb 数据库到文件:

dump.sh

#!/bin/bash


EXPORT_TOOL_PATH=/home/zjw/mongo/mongodb-linux-x86_64-2.6.1/bin/mongoexport


datetime="`date +%Y%m%d`"


function mk_dir()
{
	ret=0
	dirname=$1
	if [ ! -d $dirname ]; then
		mkdir $dirname
	else
		ret=1
	fi

	return $ret
}

function check_file_exist()
{
	ret=0

	$filename=$1

	if [ ! -f $filename ]; then
		ret=0
	else
		ret=1
	fi

	return $ret

}
mk_dir $datetime

mk_dir_ret=$?

EXPORT_DEST_PATH='./'$datetime'/gameData.json'

HOST=$1
PORT=$2

$EXPORT_TOOL_PATH --host $HOST --port $PORT -d blog -c gameData -o $EXPORT_DEST_PATH


echo "export gameData finished"


调用shell:

sh  dump.sh  192.168.1.6  27017



导出mysql数据库 到文件:

dump.sh

#!/usr/bin/bash


dumpStorDir=$1

dumpDatabaseName=$2

dumpHost=$3

mysql_user=$4

mysql_passwd=$5

datetime="`date +%Y%m%d`"

#echo $datetime


##create a directory named by datetime

function mk_new_dir()
{
	ret=0

	dirname=$1
	
	if [ ! -d $dirname ]; then
		mkdir $dirname
	else
		ret=1
	fi

	return $ret
}


function check_file_exist()
{
	ret=0

	$filename=$1
	if [ ! -f $filename ];then
		ret=0
	else
		ret=1
	fi
	
	return $ret
}

mk_new_dir $datetime

#get return value

mk_dir_ret=$?

dump_fileDir_Name=${dumpStorDir}'/'${datetime}'/'${dumpDatabaseName}'.sql'

mysqldump -h $dumpHost -u$mysql_user -p$mysql_passwd $dumpDatabaseName > $dump_fileDir_Name

echo "dump sql datas success."



调用shell:

sh dump.sh 192.168.1.6 root root php gameData.sql

你可能感兴趣的:(shell)