Oracle数据库导出
#!/bin/sh
export ORACLE_BASE=/data/u01/app/oracle
export ORACLE_HOME=/data/u01/app/oracle/product/11.2.0/dbhome_1
export ORACLE_SID=sygas
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH:.
export PATH=$ORACLE_HOME/bin:$PATH:.
export LANG=en_US.UTF-8
export NLS_LANG="AMERICAN_AMERICA".AL32UTF8
# oracle用户的系统环境变量路径:/home/oracle/.bash_profile
date=`date +%Y_%m_%d` #获取系统当前日期时间
days=7 #设置删除7天之前的备份文件
orsid=10.8.1.37:1521/orcl #Oracle数据库服务器IP、端口、SID
orowner=sygas #备份此用户下面的数据
bakuser=sygas #用此用户来执行备份,必须要有备份操作的权限
bakpass=orcl #执行备注的用户密码
bakdir=/data/back/oracle_database_back #备份文件路径,需要提前创建好
bakdata=$orowner"_"$date.dmp #备份数据库名称
baklog=$orowner"_"$date.log #备份执行时候生成的日志文件名称
ordatabak=$orowner"_"$date.tar.gz #最后保存的Oracle数据库备份文件
cd $bakdir #进入备份目录
mkdir -p $orowner #按需要备份的Oracle用户创建目录
cd $orowner #进入目录
exp $bakuser/$bakpass@$orsid grants=y owner=$orowner file=$bakdir/$orowner/$bakdata log=$bakdir/$orowner/$baklog #执行备份
tar -zcvf $ordatabak $bakdata $baklog #压缩备份文件和日志文件
find $bakdir/$orowner -type f -name "*.log" -exec rm {} \; #删除备份文件
find $bakdir/$orowner -type f -name "*.dmp" -exec rm {} \; #删除日志文件
find $bakdir/$orowner -type f -name "*.tar.gz" -mtime +$days -exec rm -rf {} \; #删除7天前的备份(注意:{} \中间有空格)
导出脚本
数据库导入
开始的准备工作:前面的步骤都一样
一:
创建表空间:
create tablespace root
datafile ‘D:\Oracle\dataSpace\root.dbf’ (你自己的存放空间)
size 50m
autoextend on;
二:
创建用户
CREATE USER root(root可以设置为自己的用户“tom”、“jerry”)
IDENTIFIED BY root
DEFAULT TABLESPACE root
TEMPORARY TABLESPACE temp; (临时表空间就叫TEMP吧,不重要)
三:
赋予权限
GRANT CONNECT TO root;
GRANT RESOURCE TO root;
GRANT dba TO root;
导入:导入时分两种方法,按照需要选择。
第一种方法:
imp amc/amc file=D:\XXXX\XXXX\XXXXXX.DMP full=y ignore=y
imp 需要导入的用户/密码@ file=‘文件地址(具体到名字后缀)’ full=y (常用)【cmd操作界面操作】
第二种方法
创建文件空间
create directory dump_dir as ‘D:\XXX\XXXX’;(在进行导入操作的时候,要把导入的dmp文件放到这个位置)
赋予读写权限
grant read,write on directory dump_dir to root
导入:
impdp root/root@orcl DIRECTORY=dump_dir DUMPFILE=XXXXXXX.DMP SCHEMAS=FRAMEWORK version=11 【cmd操作界面操作,注意文件空间名一致(标红)】
报错处理:
1:【IMP-00038: 无法转换为环境字符集句柄IMP-00000: 未成功终止导入】方法一无法使用,使用方法二
2:【ORA-39002: 操作无效ORA-39165: 未找到方案 】前面设置的用户与源文件用户名不一致,无法导入,
解决方法:
1创建与源文件一致的用户空间进行导入
2使用【impdp root/root@orcl remap_schema=【源文件用户名】:【现空间用户名】directory=root(文件命名空间) dumpfile=XXXXXXXXXXXXXXXXX.DMP】导入
原文链接:https://blog.csdn.net/m0_54521957/article/details/125291329
数据库导出文件定时上传至百度网盘
t@syrq-server-0037 ~]# vim uploadBaiduCloud.sh
#!/bin/bash
#########常量区##############
tokenFile=~/.baiduDiskTokenFile
#########变量区##############
filePath=${1}
uploadFilePath=${2}
fileSize=$(ls -l ${filePath} | awk '{print $5}')
fileSplitPath="/tmp/bdyDiskUpload/$(uuidgen | sed 's/-//g')/"
refreshAccessToken="https://openapi.baidu.com/oauth/2.0/token?grant_type=refresh_token&"
accessToken=
preCreateUrl=
uploadUrl=
createUrl=
splitFileMd5=
fileMd5=
uploadId=
###########函数区#############
#打印输出
function wlog() {
content_str=$1
var_color=$2
var_curr_timestamp=$(date "+%Y-%m-%d %H:%M:%S.%N" | cut -b 1-23)
content_echo_str=""
## 判断参数1 是否是空字符串
if [ "x${content_str}" == "x" ]; then
return
else
content_str="[${var_curr_timestamp}] ${content_str}"
fi
content_echo_str="${content_str}"
## 判断颜色
if [ "${var_color}" == "green" ]; then
content_echo_str="\033[32m${content_str}\033[0m"
elif [ "${var_color}" == "yellow" ]; then
content_echo_str="\033[33m${content_str}\033[0m"
elif [ "${var_color}" == "red" ]; then
content_echo_str="\033[1;41;33m${content_str}\033[0m"
fi
## 打印输出
echo -e "${content_echo_str}"
}
function refreshAccessToken() {
refreshToken=$(cat ${tokenFile} | grep -E 'refreshToken')
refreshToken=$(echo ${refreshToken#*=} | tr -d '"')
appKey=$(cat ${tokenFile} | grep -E 'appKey')
appKey=$(echo ${appKey#*=} | tr -d '"')
secretKey=$(cat ${tokenFile} | grep -E 'secretKey')
secretKey=$(echo ${secretKey#*=} | tr -d '"')
if [ x"${refreshToken}" = x ]; then
wlog "refreshToken不存在,程序退出" red
exit
fi
if [ x"${appKey}" = x ]; then
wlog "appKey不存在,程序退出" red
exit
fi
if [ x"${secretKey}" = x ]; then
wlog "secretKey不存在,程序退出" red
exit
fi
refreshAccessTokenResult=$(curl "${refreshAccessToken}refresh_token=${refreshToken}&client_id=${appKey}&client_secret=${secretKey}")
wlog "刷新token结果:${refreshAccessTokenResult}" green
newAccessToken=$(echo "${refreshAccessTokenResult}" | grep -oP "access_token\S{1,}?," | tr -d '\,' | awk -F ':' '{print $2}')
newRefreshToken=$(echo "${refreshAccessTokenResult}" | grep -oP "refresh_token\S{1,}?," | tr -d '\,' | awk -F ':' '{print $2}')
newExpiresIn=$(date -d "${DATE} 20 days" "+%Y%m%d")
if [ x"${newAccessToken}" = x ] || [ x"${newRefreshToken}" = x ]; then
wlog "获取新的Token失败" red
exit
fi
wlog "新的newAccessToken结果:${newAccessToken}" green
wlog "新的newRefreshToken结果:${newRefreshToken}" green
sed -i "s#refreshToken=.*#refreshToken=${newRefreshToken}#" ~/.baiduDiskTokenFile
sed -i "s#accessToken=.*#accessToken=${newAccessToken}#" ~/.baiduDiskTokenFile
sed -i "s#expiresDate=.*#expiresDate=${newExpiresIn}#" ~/.baiduDiskTokenFile
}
function getAccessToken() {
expiresDate=$(cat ${tokenFile} | grep -E 'expiresDate')
expiresDate=$(echo ${expiresDate#*=} | tr -d '"')
today="$(date +%Y%m%d)"
if [ x"${expiresDate}" = x ] || [[ ${today} > ${expiresDate} ]]; then
echo "Token可能已过期,刷新获取新的Token"
refreshAccessToken
fi
if [ -f ${tokenFile} ]; then
accessToken=$(cat ${tokenFile} | grep -E 'accessToken')
accessToken=$(echo ${accessToken#*=} | tr -d '"')
if [ x"${accessToken}" = x ]; then
wlog "accessToken不存在,退出" red
exit
fi
else
wlog "accessToken不存在,退出" red
exit
fi
}
function initBaseUrl() {
preCreateUrl="http://pan.baidu.com/rest/2.0/xpan/file?method=precreate&access_token=${accessToken}"
uploadUrl="https://d.pcs.baidu.com/rest/2.0/pcs/superfile2?method=upload&access_token=${accessToken}&type=tmpfile&"
createUrl="https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${accessToken}"
}
function generateSubFile() {
mkdir -p ${fileSplitPath}
split ${filePath} -b 4M "${fileSplitPath}sFile_"
splitFileMd5=$(md5sum ${fileSplitPath}sFile_* | awk '{print $1}' | tr '\n' ',' | sed 's/,/","/g')
splitFileMd5="[\"${splitFileMd5/%\",\"/}\"]"
wlog "获取分片的文件MD5:${splitFileMd5}" green
fileMd5=$(md5sum ${filePath} | awk '{print $1}')
wlog "获取文件MD5:${fileMd5}" green
}
function preCreateUpload() {
preCreateResult=$(curl "${preCreateUrl}" -d "path=${uploadFilePath}&size=${fileSize}&isdir=0&autoinit=1&rtype=3&block_list=${splitFileMd5}&content-md5=${fileMd5}" -H User-Agent: pan.baidu.com)
wlog "预上传请求结果:${preCreateResult}" green
resultCode=$(echo "${preCreateResult}" | grep -oP "errno\S{1,}?," | tr -d '\,' | awk -F ':' '{print $2}')
if [ "${resultCode}" -ne "0" ]; then
wlog "预上传请求失败!错误码:${resultCode}" red
wlog "可参考官方文档对照错误码:https://pan.baidu.com/union/doc/3ksg0s9r7#%E9%94%99%E8%AF%AF%E7%A0%81" red
exit
fi
uploadId=$(echo ${preCreateResult} | grep -oP "uploadid\S{1,}?\," | tr -d '"\,' | awk -F ':' '{print $2}')
}
function upload() {
idx=0
for itFile in ${fileSplitPath}*; do
if [ -f "${itFile}" ]; then
uploadResult=$(curl -F "file=@${itFile}" "${uploadUrl}path=${uploadFilePath}&uploadid=${uploadId}&partseq=${idx}")
wlog "第${idx}个分片文件上传结果:${uploadResult}" green
##提取返回码
resultCode=$(echo "${uploadResult}" | grep -oP "error_code\S{1,}?," | tr -d '\,' | awk -F ':' '{print $2}')
if [ -n "${resultCode}" ]; then
wlog "分片上传请求失败!错误码:${resultCode}" red
wlog "可参考官方文档对照错误码:https://pan.baidu.com/union/doc/nksg0s9vi#%E9%94%99%E8%AF%AF%E7%A0%81" red
exit
fi
idx=$((${idx} + 1))
fi
done
}
function createFile() {
createFileResult=$(curl "${createUrl}" -d "path=${uploadFilePath}&size=${fileSize}&isdir=0&rtype=3&uploadid=${uploadId}&block_list=${splitFileMd5}" -H User-Agent: pan.baidu.com)
wlog "创建文件结果:${createFileResult}" green
##提取返回码
resultCode=$(echo "${createFileResult}" | grep -oP "errno\S{1,}?," | tr -d '\,' | awk -F ':' '{print $2}')
if [ "${resultCode}" -ne "0" ]; then
wlog "创建文件请求失败!错误码:${resultCode}" red
wlog "可参考官方文档对照错误码:https://pan.baidu.com/union/doc/rksg0sa17#%E9%94%99%E8%AF%AF%E7%A0%81" red
exit
fi
}
function success() {
wlog "文件上传成功" green
rm -rf ${fileSplitPath}
wlog "文件夹${fileSplitPath}已清理" green
}
function main() {
getAccessToken
initBaseUrl
generateSubFile
preCreateUpload
upload
createFile
success
}
main
~
脚本出处:https://blog.csdn.net/zhaojiyuan1024/article/details/125465229#comments_22780649