-
-
Save suzukimilanpaak/405078 to your computer and use it in GitHub Desktop.
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
# - Description | |
# This program enables you to create a buck up with date in its name, | |
# which contains files symbolic links in copy source refers. | |
#-------------------- | |
# Usage | |
#-------------------- | |
# $ bak /path/to/source /path/to/dest | |
#!/bin/sh | |
exec ruby -S -x $0 "$@" | |
#! ruby | |
# Show usage | |
def usage | |
puts "Usage: #$0 SOURCE DEST" | |
puts "Copy SOURCE to DEST." | |
exit 1 | |
end | |
require 'fileutils' | |
$today = ".#{Time.now.strftime("%Y%m%d")}" | |
# Extends FileUtils::Entry_ in order to add today's date to file path. | |
class Ent < FileUtils::Entry_ | |
def initialize(a, b = nil, deref = false) | |
@prefix = @rel = @path = nil | |
if b | |
@prefix = a +$today | |
@rel = b.split('/').map!{|v| v + $today}.join('/') | |
else | |
@path = a +$today | |
end | |
#p path | |
@deref = deref | |
@stat = nil | |
@lstat = nil | |
end | |
end | |
class Backup | |
include FileUtils | |
def self.copy_and_rename_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false) | |
# Source => Use plain FileUtils::Entry_ | |
FileUtils::Entry_.new(src, nil, dereference_root).traverse do |ent| | |
#p ent | |
# Destination => Use class Ent to rewirte file path. | |
destent = Ent.new(dest, ent.rel, false) | |
File.unlink destent.path if remove_destination && File.file?(destent.path) | |
ent.copy destent.path | |
ent.copy_metadata destent.path if preserve | |
end | |
end | |
end | |
# If number of given Argument Vector is other than two | |
if ARGV.size != 2 | |
usage | |
else | |
Backup.copy_and_rename_entry(ARGV[0], ARGV[1], true) | |
end | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment