2013年9月18日水曜日

CentOSにH2 Databaseをインストール

概要

H2 Databaseをインストールすることがメインなので、今回はパーミッション、アクセス権限は考えない

メモ

Javaをインストール


#!/bin/sh
# ダウンロードURL
DownloadURL="http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz"
# インストール先
InstallDirectory="/usr/local/java"
# シンボリック・リンク
JavaSymbolicLink=$InstallDirectory"/latest"
# ダウンロードファイル名
DownloadFileName=${DownloadURL##*/}
LogFileName="$DownloadFileName""_install_""`date +%Y%m%d%H%M`.log"
### ダウンロード処理
#
DownLoadFilePath="$InstallDirectory"/"$DownloadFileName"
LogFilePath="$InstallDirectory"/"$LogFileName"
echo "========== $DownloadFileName install start ==========" >> $LogFilePath
# ディレクトリチェック
if [ ! -e $InstallDirectory ]; then
mkdir -p $InstallDirectory
if [ $? -gt 0 ]; then
echo "Cannot create $InstallDirectory"
break;
fi
fi
# ファイルダウンロード
echo "========== ファイルダウンロード ==========" >> $LogFilePath
if [ ! -e $DownLoadFilePath ]; then
wget --no-cookies --no-check-certificate \
--header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com; oraclelicense=accept-securebackup-cookie" $DownloadURL \
-O $DownLoadFilePath \
-a $LogFilePath
if [ $? -gt 0 ]; then
echo "Cannot download $DownloadURL"
rm -rf $DownLoadFilePath
break;
fi
else
echo "Already download $DownloadURL"
fi
### 解凍処理
#
echo "========== 解凍処理 ==========" >> $LogFilePath
# 解凍したフォルダ名
UnzippedDirectoryName=$(tar tfz $DownLoadFilePath | head -1)
UnzippedDirectoryPath=$InstallDirectory"/"$UnzippedDirectoryName
if [ ! -e $UnzippedDirectoryPath ]; then
tar zxvfC $DownLoadFilePath $InstallDirectory >> $LogFilePath
else
echo "Already install $UnzippedDirectoryPath"
fi
### シンボリックリンク作成
#
ln -snf $UnzippedDirectoryPath $JavaSymbolicLink
echo "========== $DownloadFileName install end ==========" >> $LogFilePath
view raw install_java.sh hosted with ❤ by GitHub

H2 DataBaseをインストール


#!/bin/sh
# ダウンロードURL
DownloadURL="http://www.h2database.com/h2-2014-08-06.zip"
# インストール先
InstallDirectory="/opt/database"
#
# ダウンロードファイル名
DownloadFileName=${DownloadURL##*/}
LogFileName="$DownloadFileName""_install_""`date +%Y%m%d%H%M`.log"
### ダウンロード処理
#
DownLoadFilePath="$InstallDirectory"/"$DownloadFileName"
LogFilePath="$InstallDirectory"/"$LogFileName"
echo "========== $DownloadFileName install start ==========" >> $LogFilePath
# ディレクトリチェック
if [ ! -e $InstallDirectory ]; then
mkdir -p $InstallDirectory
if [ $? -gt 0 ]; then
echo "Cannot create $InstallDirectory"
break;
fi
fi
echo "========== ファイルダウンロード ==========" >> $LogFilePath
if [ ! -e $DownLoadFilePath ]; then
wget $DownloadURL \
-O $DownLoadFilePath \
-a $LogFilePath
if [ $? -gt 0 ]; then
echo "Cannot download $DownloadURL"
rm -rf $DownLoadFilePath
break;
fi
else
echo "Already download $DownloadURL"
fi
### 解凍処理
#
echo "========== 解凍処理 ==========" >> $LogFilePath
# 解凍したフォルダ名
UnzippedDirectoryPath=$InstallDirectory"/h2"
if [ ! -e $UnzippedDirectoryPath ]; then
unzip -n $DownLoadFilePath -d $InstallDirectory >> $LogFilePath
else
echo "Already install $UnzippedDirectoryPath"
fi
echo "========== $DownloadFileName install end ==========" >> $LogFilePath
view raw install_h2.sh hosted with ❤ by GitHub

H2 DataBaseに接続

# H2 DataBaseに起動
/usr/local/java/latest/bin/java -cp /opt/database/h2/bin/*.jar org.h2.tools.Server

Welcome to H2 Shell 1.3.173 (2013-07-28)
Exit with Ctrl+C
[Enter]   jdbc:h2:~/test
URL
[Enter]   org.h2.Driver
Driver
[Enter]   sa
User
[Enter]   Hide
Password
Password
Connected
Commands are case insensitive; SQL statements end with ';'
help or ?      Display this help
list           Toggle result list / stack trace mode
maxwidth       Set maximum column width (default is 100)
autocommit     Enable or disable autocommit
history        Show the last 20 statements
quit or exit   Close the connection and exit

sql> SELECT * FROM INFORMATION_SCHEMA.CATALOGS ;
CATALOG_NAME
TEST
(1 row, 7 ms)
sql> exit;
Connection closed

2013年9月9日月曜日

xmlからログ設定を取得

概要

ログ出力する設定をXMLから取得する関数を作成する。

・初期設定はpsに記述する
・XMLにアペンダを記述して選べるようにする

関数


### 初期値の設定
# ログファイルパス
Remove-Variable -Name LogFilePath -Force -ea silentlycontinue
Set-Variable -Name LogFilePath -Value "common.log" -Option ReadOnly -Scope "Global"
# ログレベル
Remove-Variable -Name LogLevel -Force -ea silentlycontinue
Set-Variable -Name LogLevel -Value 1 -Option ReadOnly -Scope "Global"
# ログの文字エンコード
Remove-Variable -Name LogEncoding -Force -ea silentlycontinue
Set-Variable -Name LogEncoding -Value "utf8" -Option ReadOnly -Scope "Global"
# ------------------------------------------------------------------
# XMLファイルよりログ出力の設定情報をセットする
# 関数名:Set-LogSetting
# 引数 :XMLFilePath XMLファイルパス
# :Appender アペンダ
# 戻り値:なし
# ------------------------------------------------------------------
function Set-LogSetting([String]$XMLFilePath, [String]$Appender){
#XMLファイルの読込
$xmlLogSetting = [XML](Get-Content $XMLFilePath -encoding "utf8")
# 指定したアペンダを取得
$params = $xmlLogSetting.configuration.appender | Where-Object{$_.name -eq $Appender}
if($params){
foreach($param In $params){
# ログファイルパス
if("file" -eq $param.name){
Set-Variable -Name LogFilePath -Value ($param.value) -Option ReadOnly -Scope "Global" -Force
}
# ログ文字エンコード
if("encode" -eq $param.name){
Set-Variable -Name LogEncoding -Value ($param.value) -Option ReadOnly -Scope "Global" -Force
}
# ログの種類
if("loglevel" -eq $param.name){
Set-Variable -Name LogLevel -Value ($param.value) -Option ReadOnly -Scope "Global" -Force
}
}
}
}

XML設定


<!--?xml version="1.0" encoding="UTF-8" ?-->
<configuration>
<appender name="unit">
<!-- ログファイル名 -->
<param name="file" value="app_unitTest.log">
<!-- ログレベル(0:デバック,1:情報,2:警告,3:エラー) -->
<param name="loglevel" value="0">
<!-- ログエンコード -->
<param name="encode" value="utf8">
</appender>
<appender name="product">
<!-- ログファイル名 -->
<param name="file" value="app.log">
<!-- ログレベル(0:デバック,1:情報,2:警告,3:エラー) -->
<param name="loglevel" value="1">
<!-- ログエンコード -->
<param name="encode" value="utf8">
</appender>
</configuration>
view raw log4ps.xml hosted with ❤ by GitHub

実行例

# XML読込
Set-LogSetting -XMLFilePath "C:\log4ps.xml" -Appender "unit"
# ログ出力
Write-DebugLog -VALUE "デバッグ"
Write-InfoLog -VALUE "情報"
Write-WarnLog -VALUE "警告"
Write-ErrorLog -VALUE "エラー" 

2013年9月7日土曜日

ファイルサイズでファイルをローテーションする

概要

ログを出し続けるとファイルサイズが大きくなりすぎて、処理が遅くなったり、ログの移動・参照もたいへんなので
ファイルサイズよりローテーションを行う関数を作成

・指定したファイルサイズ以上になったらローテーションを行う
・ローテーションする回数も指定する
・ローテーションしたファイル名は「ログファイル名」+「.」+「回数」

関数


# ------------------------------------------------------------------
# ファイルサイズでファイルのローテーションを行う
# 関数名:Rotate-FileSize
# 引数 :FilePath ファイルパス
# :RollingFileSize 最大ファイルサイズ(デフォルト:10MB)
# :MaxBackupIndex 最大バックアップログファイル数(デフォルト:10)
# 戻り値:なし
# ------------------------------------------------------------------
function Rotate-FileSize([String]$FilePath, [Int]$RollingFileSize = 10MB, [Int]$MaxBackupIndex = 10){
# ファイルが存在する場合
if(Test-Path -LiteralPath $FilePath -PathType Leaf){
# ファイルサイズ
$filesize = $(Get-ItemProperty $FilePath).Length
# ローテート対象
if($filesize -gt $RollingFileSize){
$sizeRollingName = (Split-Path $FilePath -Leaf) + "."
$sizeRollingPath = Join-Path -Path (Split-Path $FilePath -Parent) -ChildPath $sizeRollingName
for($i = $MaxBackupIndex - 1;$i -ge 1;$i--){
if(Test-Path -LiteralPath ($sizeRollingPath + $i) -PathType Leaf){
Move-Item -LiteralPath ($sizeRollingPath + $i) -Destination ($sizeRollingPath + ($i + 1)) -Force
}
}
Move-Item -LiteralPath $FilePath -Destination ($sizeRollingPath + 1) -Force
}
}
}

日付でファイルをローテーションする

概要

日付よりローテーションを行う関数を作成

・ファイルの作成日が現在の日付でない場合にローテーションを行う
・ローテーションしたファイル名は「ログファイル名」+「.」+「日付フォーマット」

関数


# ------------------------------------------------------------------
# 日付でファイルのローテーションを行う
# 関数名:Rotate-Daily
# 引数 :FilePath ファイルパス
# :DailyRollingFormat 日付フォーマット(デフォルト:yyyy-MM-dd)
# 戻り値:なし
# ------------------------------------------------------------------
function Rotate-Daily([String]$FilePath,[String]$DailyRollingFormat = "yyyy-MM-dd"){
# ファイルが存在する場合
if(Test-Path -LiteralPath $FilePath -PathType Leaf){
# ファイル作成日
$creationDate = $(Get-ItemProperty $FilePath).CreationTime.ToString($DailyRollingFormat)
# 現在日付
$currentDate = Get-Date -Format $DailyRollingFormat
# ローテート対象
if($creationDate -ne $currentDate){
$dailyRollingName = (Split-Path $FilePath -Leaf) + "." + $creationDate
$dailyRollingPath = Join-Path -path (Split-Path $FilePath -Parent) -ChildPath $dailyRollingName
Move-Item -LiteralPath $FilePath -Destination $dailyRollingPath -Force
}
}
}

2013年9月3日火曜日

ファイル名に角括弧[]がついてるとRename-Itemで失敗する

概要

ファイル名に角括弧[]がついてるとRename-Itemで失敗するので、Move-Itemを代用する

関数

# ------------------------------------------------------------------
# ファイル名をリネームする
# 関数名:Rename-File
# 引数  :FilePath 名前を変更するファイルパス
#       :NewFileName 新しいファイル名前
# 戻り値:なし
# ------------------------------------------------------------------
function Rename-File([String]$FilePath, [String]$NewFileName){
  if(Test-Path -LiteralPath $FilePath -PathType Leaf){
    $newFilePath = Join-Path -Path (Split-Path $FilePath -Parent) -ChildPath $NewFileName
    Move-Item -LiteralPath $FilePath -Destination $newFilePath
  }else{
    Write-Host "ファイルが存在しません。ファイル名[ $FilePath ]"
  }
}
 
# ------------------------------------------------------------------
# フォルダ名をリネームする
# 関数名:Rename-Folder
# 引数  :FolderPath 名前を変更するフォルダパス
#       :NewFolderName 新しいフォルダ名前
# 戻り値:なし
# ------------------------------------------------------------------
function Rename-Folder([String]$FolderPath, [String]$NewFolderName){
  if(Test-Path -LiteralPath $FolderPath -PathType Container){
    $newFolderPath = Join-Path -Path (Split-Path $FolderPath -Parent) -ChildPath $NewFolderName
    if(Test-Path -LiteralPath $FolderPath -PathType Container){
      Write-Host "既に存在するフォルダを作成することはできません。フォルダー名[ $FolderPath ]"
    }else{
      Move-Item -LiteralPath $FolderPath -Destination $newFolderPath
    }
  }else{
    Write-Host "フォルダが存在しません。フォルダ名[ $FolderPath ]"
  }
}

実行例

Rename-File -FilePath "C:\[20130903]新規要件定義\[20130903]Webログイン.xls" -NewFileName "Webログイン.xls"
Rename-Folder -FolderPath "C:\[20130903]新規要件定義" -NewFolderName "[20130903]要件定義"