Created
July 26, 2020 13:31
-
-
Save yamaimo/93eea0dd2be917a80030d883279105ed to your computer and use it in GitHub Desktop.
Simple Snapshot Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
USAGE =<<END_OF_USAGE | |
$ ./snapshot <target>.<ext> | |
Save snapshot of taget into HISTORY_DIR/<target>-yyyymmdd.<ext> | |
END_OF_USAGE | |
# 設定 -------------------- | |
HISTORY_DIR = './history' | |
LAST_SUFFIX = '-last' | |
#-------------------- | |
# 引数処理 | |
if ARGV.empty? | |
puts USAGE | |
exit 1 | |
end | |
target_file = ARGV[0] | |
target_base = File.basename(target_file, ".*") | |
target_ext = File.extname(target_file) | |
last_file = "#{HISTORY_DIR}/#{target_base}#{LAST_SUFFIX}#{target_ext}" | |
# ファイルチェック | |
if File.exist?(last_file) | |
if FileUtils.cmp(target_file, last_file) | |
puts "変更がありません。" | |
exit 1 | |
end | |
end | |
# 保存先 | |
FileUtils.makedirs(HISTORY_DIR) | |
today = Time.now | |
today_str = sprintf "%04d%02d%02d", today.year, today.month, today.day | |
snapshot_file = "#{HISTORY_DIR}/#{target_base}-#{today_str}#{target_ext}" | |
# ファイルバックアップ | |
FileUtils.cp target_file, snapshot_file | |
FileUtils.cp target_file, last_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment