テキスト系のファイルをバックアップするために、Gitレポジトリを利用することがあるのですが、使いまわせるようにbashで簡単なスクリプトを書いてみました。
単純なバックアップ目的なので、branchは固定でmasterを指定しています。
#!/bin/bash
checkout(){
local url=${1}
local checkout_dir=${2}
if [ ! -d $checkout_dir ]
then
git clone $url $checkout_dir
else
cd $checkout_dir
git pull
fi
}
commit(){
local commit_target_dir=${1}
local message=${2}
cd $commit_target_dir
diff_count=$(echo `git status -s | wc -l`)
if [ $diff_count -ne 0 ]; then
git add -A
git commit -m $message
git push origin master
fi
}
### 使用例 ###
USER=...
PASSWORD=...
# URL組み立て
URL=https://$USER:$PASSWORD@githost/project
checkout $URL "backup_directory"
### do something in backup_directory...
commit "backup_directory" "Auto commit by bash"
ちなみに筆者は、バックアップを下記の手順で取ってgitへ入れています。参考になれば、、、
- 上記のcheckout関数を用いて、gitレポジトリからプロジェクトをclone(既にclone済みの場合はpull)
- リモートホストからチェックアウトしたディレクトリへファイルをrsyncで同期
- 上記のcommit関数を用いて、gitレポジトリへpush
コメント