PowerShell 自动备份oracle并上传到ftp(networker备份oracle)怎么可以错过

随心笔谈2年前发布 编辑
154 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买


# Author: Edward.Zhou — <edgeman_03@163.com>
# Purpose: Windows平台下采用PowerShell使用exp自动备份oracle并上传备份到ftp,并根据日期自动保留所需备份
# Created: 2015/8/8

$env:ORACLE_SID=”orcl”
$env:NLS_LANG=”AMERICAN_AMERICA.AL32UTF8″

$NOWDATE=Get-Date -UFormat %Y_%m_%d
$NOWTIME=Get-Date -UFormat %Y%m%d%H%M%S
$OLDDATE=((Get-Date).AddDays(-1)).ToString(‘yyyy_MM_dd’)
$FTPOLDDATE=((Get-Date).AddDays(-7)).ToString(‘yyyy_MM_dd’)

#建立备份目录基本环境
$BACKUPDIR=”d:\bak\dmp”

if ((Test-Path -Path $BACKUPDIR) -eq $false) {
New-Item -Type directory -Path $BACKUPDIR
}
if ((Test-Path -Path $BACKUPDIR\$NOWDATE) -eq $false) {
New-Item -Type directory -Path $BACKUPDIR\$NOWDATE
}
if ((Test-Path -Path $BACKUPDIR\$OLDDATE) -eq $true) {
Remove-Item -Recurse -Force -Path $BACKUPDIR\$OLDDATE
}

#exp备份
$USERNAME=”system”
$PASSWORD=”system”

exp $USERNAME/$PASSWORD file=$BACKUPDIR\$NOWDATE\$env:COMPUTERNAME-$env:ORACLE_SID-fullbackup_$NOWTIME.dmp log=$BACKUPDIR\$NOWDATE\$env:COMPUTERNAME-$env:ORACLE_SID-fullbackup_$NOWTIME.log full=y direct=y consistent=y buffer=102400

#上传备份至FTP方法一
Import-Module PSFTP
$FTP_HOST=”1.1.1.1″
$FTP_USERNAME=”backup”
$FTP_PASSWORD=ConvertTo-SecureString “backup” -AsPlainText -Force
$Credentials=New-Object System.Management.Automation.PSCredential($FTP_USERNAME,$FTP_PASSWORD)
Set-FTPConnection -Credentials $Credentials -Server ftp://$FTP_HOST -Session oradmpSession -UsePassive -UseBinary -KeepAlive -ignoreCert
$Session=Get-FTPConnection -Session oradmpSession

if ($Session -eq $null) {
Set-FTPConnection -Credentials $Credentials -Server ftp://$FTP_HOST -Session oradmpSession -UsePassive -UseBinary -KeepAlive -ignoreCert
}
if ((Get-FTPChildItem -Session $Session -Path / -ErrorAction SilentlyContinue -Filter oradmp) -eq $null) {
New-FTPItem -Session $Session -Path / -Name “oradmp”
}
if ((Get-FTPChildItem -Session $Session -Path /oradmp -ErrorAction SilentlyContinue -Filter $env:COMPUTERNAME) -eq $null) {
New-FTPItem -Session $Session -Path /oradmp -Name $env:COMPUTERNAME
}
if ((Get-FTPChildItem -Session $Session -Path /oradmp/$env:COMPUTERNAME -ErrorAction SilentlyContinue -Filter $NOWDATE) -eq $null) {
New-FTPItem -Session $Session -Path /oradmp/$env:COMPUTERNAME -Name $NOWDATE
}
foreach($Filename in (Get-ChildItem -Path $BACKUPDIR\$NOWDATE -Recurse))
{
Add-FTPItem -Session $Session -Overwrite -BufferSize 102400 -Path /oradmp/$env:COMPUTERNAME/$NOWDATE -LocalPath $BACKUPDIR\$NOWDATE\$Filename
}
if ((Get-FTPChildItem -Session $Session -Path /oradmp/$env:COMPUTERNAME -ErrorAction SilentlyContinue -Filter $FTPOLDDATE) -ne $null) {
Remove-FTPItem -Path /oradmp/$Env:COMPUTERNAME/$FTPOLDDATE -Session $Session -Recurse
}

#上传备份至FTP方法二
#$FTP_HOST=”1.1.1.1″
#$FTP_USERNAME=”backup”
#$FTP_PASSWORD=”backup”
#
#Set-Location -Path $BACKUPDIR
#
#Write-Output “open $FTP_HOST” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “user $FTP_USERNAME $FTP_PASSWORD” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “bin” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “mkdir oradmp” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “cd oradmp” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “mkdir $Env:COMPUTERNAME” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “cd $Env:COMPUTERNAME” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “mkdir $NOWDATE” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “cd $NOWDATE” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “lcd $NOWDATE” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “mput *” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “cd ..\$FTPOLDDATE” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “mdelete *” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “cd ..” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “rmdir $FTPOLDDATE” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#Write-Output “bye” | Out-File -Append -Force -Encoding Default -FilePath $BACKUPDIR\ftp.cfg
#ftp -i -n -v -s:$BACKUPDIR\ftp.cfg
#Remove-Item -Force -Path $BACKUPDIR\ftp.cfg

© 版权声明

相关文章