Skip to content

Instantly share code, notes, and snippets.

@yamaimo
Created July 26, 2020 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaimo/93eea0dd2be917a80030d883279105ed to your computer and use it in GitHub Desktop.
Save yamaimo/93eea0dd2be917a80030d883279105ed to your computer and use it in GitHub Desktop.
Simple Snapshot Script
#!/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