Created
August 19, 2012 15:36
-
-
Save mori-dev/3395489 to your computer and use it in GitHub Desktop.
private-backup
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 | |
# -*- encoding: utf-8 -*- | |
# ■要件 | |
# | |
# emacs で f12 キー押下で編集中のファイルをディレクトリ構造ごとバックアップしたい | |
# ターミナルから引数で指定したファイル/ディレクトリをディレクトリ構造ごとバックアップしたい | |
# | |
# ■使い方 | |
# | |
# ■■準備 | |
# | |
# デフォルトでは "~/.bk" 以下にバックアップが作成される。変更する場合は、ディレクトリ名を | |
# 記載した "~/.private_backup_command" ファイルを作成する | |
# | |
# ■■ターミナルから利用する場合 | |
# | |
# 例1 (ファイルの指定): | |
# private-backup /foo/bar/baz.txt | |
# 例2 (ディレクトリの指定): | |
# private-backup /foo/bar | |
# | |
# /foo/bar/baz.txt なら、"{起点ディレクトリ}/{年月日時間}/foo/bar/baz.txt" に cp する | |
# /foo/bar なら、"{起点ディレクトリ}/{年月日時間}/foo/bar" に cp -r する | |
# | |
# ■■emacs から利用する場合 | |
# | |
# 設定ファイル(.emacs, init.el など)へ以下を追記する。 | |
# | |
# (defun private-backup-command () | |
# (interactive) | |
# (unless (executable-find "private-backup") | |
# (error "private-backup command not found. see https://gist.github.com/3395489")) | |
# (case (call-process-shell-command (executable-find "private-backup") nil nil nil buffer-file-name) | |
# ((0) (message "OK! private-backup success.")) | |
# (otherwise (message "NG. private-backup fail.")))) | |
# | |
# (global-set-key (kbd "<f12>") 'private-backup-command) | |
require "fileutils" | |
if ARGV.size === 1 then | |
from_arg = ARGV.shift | |
else | |
puts "Usage: #{File.basename($PROGRAM_NAME)} file_or_directory" | |
exit 1 | |
end | |
cofig_file = "~/.private_backup_command" | |
if File.exists?(cofig_file_path = File.expand_path(cofig_file)) | |
base_dir = File.read cofig_file_path | |
base_dir.chomp! | |
else | |
base_dir = "~/.bk" | |
end | |
from = File.expand_path(from_arg) | |
to = File.join(File.expand_path(base_dir), Time.now.strftime("%Y-%m-%d_%H-%M-%S"), from) | |
FileUtils.mkdir_p File.dirname(to) | |
FileUtils.send (FileTest.directory?(from) ? "cp_r" : "cp"), from, to |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment