#!/bin/sh
# ファイルサイズを判定してローテートするサンプル
# << SCRIPT SUB SECTION >>
##### ファイルサイズを判定してローテート
# $1:対象ファイル
# $2:ローテート条件
rotate() {
file_size=`ls -lk --time-style='+%Y%m%d' $1 | awk '{print $5}'`
file_time=`ls -lk --time-style='+%Y%m%d' $1 | awk '{print $6}'`
# ファイルサイズがローテート条件を超えたら
if [ ${file_size} -gt $2 ]; then
tar -zcf $1.${file_time}.tar.gz $1
# 0バイト化
:> $1
fi
}
# << SCRIPT MAIN SECTION >>
TRAGET_DIR=/home/operation # ローテート対象ディレクトリ
TRAGET_FILE=rsync.log # ローテート対象ファイル
THRESHOLD=20 # KBで指定
BASE=`pwd`
cd ${TRAGET_DIR}
rotate ${TRAGET_FILE} ${THRESHOLD}
ls -l ${TRAGET_FILE}*
cd ${BASE}
exit 0
2011年3月31日木曜日
シェルスクリプトサンプル:ファイルローテート
2011年3月10日木曜日
ApacheのBasic認証設定
今さらですがApacheのBasic認証設定のメモ。 OS: CentOS 5.7
■.htpasswdの作成
[root@f1 ~]# /usr/bin/htpasswd -c /etc/httpd/.htpasswd secret New password: ←yak***** Re-type new password: ←yak***** Adding password for user secret [root@f1 ~]#※OSユーザにsecretが登録されている必要はない。
■httpd.confの修正
[root@f1 ~]# vi /etc/httpd/conf/httpd.conf (中略) ServerName www.dummy84.com DocumentRoot /files3/www AllowOverride AuthConfig ←Noneから変更(.htaccessが無ければ認証はかからない) Options ExecCGI FollowSymLinks Order allow,deny Allow from all Dav On
■Apache再起動
[root@f1 ~]# /usr/sbin/apachectl stop [root@f1 ~]# /usr/sbin/apachectl -f /etc/httpd/conf/httpd_vhost.conf
■.htaccessの配置
[root@f1 ~]# vi /files3/www/.htaccess [root@f1 ~]# cat /files3/www/.htaccess AuthType Basic AuthUserFile /etc/httpd/.htpasswd AuthGroupFile /dev/null AuthName "Please Enter Your Name and Password" require valid-user [root@f1 ~]# chmod 444 /files3/www/.htaccess
こちらの情報が何かのお役にたてれば幸いです。m(_ _)m
2011年2月1日火曜日
シェルスクリプトサンプル:一括文字コード変換
#!/bin/sh
# 作業ディレクトリ(現在地)
BASE=`pwd`
# 変換対象元ディレクトリ
DIR=/home/efiles3/public/iw/072_STG-Web
# 変換対象ファイルの拡張子を指定する。
FILE_TYPE=".txt"
# 変換先文字コードオプション(UTF-8:-w SJIS:-s EUC:-e)
CHARA_CODE="-s"
# 出力先ディレクトリ作成
if [ ${CHARA_CODE} = "-w" ];then
OUTPUT_DIR="output_utf-8"
elif [ ${CHARA_CODE} = "-s" ];then
OUTPUT_DIR="output_sjis"
elif [ ${CHARA_CODE} = "-e" ];then
OUTPUT_DIR="output_euc"
else
echo "Character code is not existing error !!"
exit 0
fi
mkdir ./$OUTPUT_DIR
# 文字コード変換
cd $DIR
for i in `ls -1` ; do
/usr/bin/nkf $CHARA_CODE $i > $BASE/$OUTPUT_DIR/$i
done
cd $BASE
exit 0
2011年1月7日金曜日
シェルスクリプトサンプル:diffの差分比較結果をファイル出力
diffコマンドを利用して、2つのファイルの差分比較を行い、その結果を ファイルに出力するシェルスクリプトのサンプルです。 Webコンテンツの移行作業において、移行前と移行後のファイルの差分が 発生していないか検証する際に便利です。
■サンプルスクリプト(sabun.sh)
[root@tanyao ~]# cat sabun.sh
#!/bin/sh
# 必ず比較対象ファイルと同階層にこのシェルを配置して実行してください!!
# 比較対象(パス指定禁止)
FILE1=file1.txt
FILE2=file2.txt
# ファイル名にスペースを含むファイルはNG扱いとする
if [ `grep " " ${FILE1} | wc -l` != 0 ] ; then
grep " " ${FILE1} > diff_NG_${FILE1}
fi
if [ `grep " " ${FILE2} | wc -l` != 0 ] ; then
grep " " ${FILE2} > diff_NG_${FILE2}
fi
# 並び替え
grep -v " " ${FILE1} | sort > sort_${FILE1}
grep -v " " ${FILE2} | sort > sort_${FILE2}
# 比較
diff -y -t -d sort_${FILE1} sort_${FILE2} > diff.txt
# 同一レコードの抽出
grep -v "<" diff.txt | grep -v ">" | grep -v "|" > diff_same.txt
# 第1ファイルにのみ存在
grep -e "<" -e "|" diff.txt | cut -d " " -f 1 > diff_only_${FILE1}
# 第2ファイルにのみ存在
grep ">" diff.txt | cut -d ">" -f 2 > diff_tmp_${FILE2}
grep "|" diff.txt | cut -d "|" -f 2 >> diff_tmp_${FILE2}
sed -e 's/^ //' diff_tmp_${FILE2} > diff_only_${FILE2}
# 作業ファイルの削除
rm -rf sort_${FILE1}
rm -rf sort_${FILE2}
rm -rf diff_tmp_${FILE2}
exit 0
[root@tanyao ~]#
■テストデータ準備
[root@tanyao ~]# cat file1.txt /var/log/httpd/ /var/log/httpd/error_log /var/log/httpd/error_log-20110103 /var/log/httpd/access_log /var/log/httpd/access_log-20110103 [root@tanyao ~]# [root@tanyao ~]# cat file2.txt /var/log/httpd/ /var/log/httpd/error_log /var/log/httpd/error_log-20110104 /var/log/httpd/access_log /var/log/httpd/access_log.1 /var/log/httpd/access_log-20110103 [root@tanyao ~]#
■使用例および結果確認
[root@tanyao ~]# ./sabun.sh [root@tanyao ~]# cat diff_only_file1.txt /var/log/httpd/error_log-20110103 [root@tanyao ~]# cat diff_only_file2.txt /var/log/httpd/access_log.1 /var/log/httpd/error_log-20110104 [root@tanyao ~]# cat diff_same.txt /var/log/httpd/ /var/log/httpd/ /var/log/httpd/access_log /var/log/httpd/access_log /var/log/httpd/access_log-20110103 /var/log/httpd/access_log-20110103 /var/log/httpd/error_log /var/log/httpd/error_log [root@tanyao ~]#
■diffオプション
-y, --side-by-side
side-by-side 出力形式を用いる。
side-by-side 形式
ファイルは 2 列に表示され、間に溝 (gutter) が置かれる。溝には以下のマーカーのいずれか 1 つが含まれる。
' ' 対応する行が共通である。つまり、両方の行が同一であるか、違いが --ignore オプションのいずれかによって無視された。
| 対応する行が異なる。両方とも完全か、両方とも不完全かである。
< ファイルは異なり、1 番目のファイルにだけこの行が含まれている。
> ファイルは異なり、2 番目のファイルにだけこの行が含まれている。
( 1 番目のファイルにだけこの行が含まれているが、違いは無視される。
) 2 番目のファイルにだけこの行が含まれているが、違いは無視される。
サイト継続にご協力ありがとうございます。こちらの情報が何かのお役にたてれば幸いです。m(_ _)m
2010年12月9日木曜日
SELinux無効化
■OSインストール後にSELinuxを無効にする /etc/selinux/configをvi等で編集し SELINUX=disabled とした後rebootする。 [root@tanyao ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - SELinux is fully disabled. SELINUX=disabled # SELINUXTYPE= type of policy in use. Possible values are: # targeted - Only targeted network daemons are protected. # strict - Full SELinux protection. SELINUXTYPE=targeted [root@tanyao ~]# [root@tanyao ~]# reboot ※ SELINUX=permissive の状態でidコマンドを実行すると下記のように表示される。 [root@tanyao ~]# id uid=0(root) gid=0(root) 所属グループ=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh ■SELinuxの動作モード確認と切り替え方法 ---------------------------------------------------- Enforcing:許可されていないアクセスを拒否する Permissive:許可されていないアクセスがあった場合でもログを出力するだけで実際にはアクセス制御を行わない Disabled:SELinux無効 ---------------------------------------------------- [root@tanyao ~]# setenforce 0 [root@tanyao ~]# getenforce Permissive [root@tanyao ~]# setenforce 1 [root@tanyao ~]# getenforce Enforcing ※ /etc/selinux/config で SELINUX=permissive または SELINUX=enforcing となっている場合。 ※ 上記コマンドは一時的な設定でありrebootすると/etc/selinux/configの設定に戻る。 ⇒ /etc/selinux/config が SELINUX=disabled に設定されている場合で上記コマンドを実行すると下記のようになる。 [root@tanyao ~]# getenforce Disabled [root@tanyao ~]# setenforce 0 setenforce: SELinux is disabled [root@tanyao ~]# setenforce 1 setenforce: SELinux is disabled
登録:
投稿 (Atom)