ファイルリスト作成し、tarで圧縮するシェルスクリプト



やりたかったこと

  1. 指定フォルダ(hoge)のファイルリスト(パーミッション、タイムスタンプ、サイズ、フルパス)を作成する(~filelist.log)
    コマンド:find /hoge_parent/hoge -ls
  2. 実行ログに処理開始時間を記録する(~execution.log)
  3. 指定フォルダ(hoge)をtar形式に圧縮する(エラーの場合は標準出力に渡す)
    コマンド:tar cfv /hoge.tar -C /hoge_parent hoge
    解説  :/hoge_parent に移動し(-C)、hogeフォルダを圧縮する。圧縮して/hoge.tarを作成する(cfv)
  4. 実行ログに処理修了時間を記録する(~execution.log)



#!/bin/bash

<< THIS_IS_COMMENT_LINE


This script makes a "tar" archive and outputs list of the archived files
and outputs a log to ensure results.

available: HP-UX / RHEL xx

caution!! user must be "root".

usage:
$1 is folder to archive
$2 is name of "tar" archive
$3 is destination to make "tar" archive
$4 is name of filelists and log

sample:
sh commontarls.sh \
/Users/t-yamamoto/Documents/_Ruby/ \
_Ruby.tar \
/Users/t-yamamoto/Documents/bkyama \
_Ruby


THIS_IS_COMMENT_LINE




# define variable
folder_to_archive="$1"
name_of_tar="$2"
destiation_of_tar="$3"
name_of_filelists_log="$4"


# to output fullname file list
find ${folder_to_archive} -ls > ${destiation_of_tar}/${name_of_filelists_log}_filelist.log




# to output start date
echo "start ${name_of_tar}" >> ${destiation_of_tar}/${name_of_filelists_log}_execution.log
date >> ${destiation_of_tar}/${name_of_filelists_log}_execution.log




# to make "tar" archive
echo command: \
tar cfv ${destiation_of_tar}/${name_of_tar} \
-C `dirname ${folder_to_archive}` `basename ${folder_to_archive}` \
>> ${destiation_of_tar}/${name_of_filelists_log}_execution.log

tar cfv ${destiation_of_tar}/${name_of_tar} \
-C `dirname ${folder_to_archive}` `basename ${folder_to_archive}` \
>> ${destiation_of_tar}/${name_of_filelists_log}_execution.log 2>&1

case `uname` in
  "Linux")
       exitcode=$?;;
  "HP-UX")
       exitcode=$status;;
esac

[ ${exitcode} -eq 0 ] && echo "status:${exitcode} ok!!" || \
echo "status:${exitcode} archive failed!!!!!!!!"




# to output end date
echo "end ${name_of_tar}" >> ${destiation_of_tar}/${name_of_filelists_log}_execution.log
date >> ${destiation_of_tar}/${name_of_filelists_log}_execution.log



exit 0
  • 参考にしたサイト

ls -lコマンドの結果をサブディレクトリ含めてフルパスで列挙したい - Qiita

シェルスクリプトで親ディレクトリのパスを取得したい – hrendoh's tech memo

tesiri.hateblo.jp

qiita.com

Bashでcase文を利用する – REONTOSANTA





終わり