/
sync-day-by-day
executable file
·80 lines (69 loc) · 1.84 KB
/
sync-day-by-day
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /usr/bin/env ruby
#
# sync-day-by-day - sync files day by day
#
# Copyright (C) 2000 Satoru Takabayashi <satoru@namazu.org>
# All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of
# the GNU General Public License version 2.
#
# original file URL: http://0xcc.net/attic/sync-day-by-day
# -----
#
# Modified by Yoshinori Ikarashi <yoosee@init.org>
# under the same license of GPLv2.
#
# https://github.com/yoosee/local_settings/blob/master/bin/sync-day-by-day
#
require 'find'
require 'date'
require 'fileutils'
def usage
puts "Usage: sync-day-by-day <source directory> <destination directory>"
exit
end
def nodir(dir)
puts "No directory: " + dir
exit 1
end
def same_file? (f1, f2)
File.symlink?(f1) == false && File.symlink?(f2) == false && \
File.file?(f1) && File.file?(f2) && \
File.size(f1) == File.size(f2) && File.mtime(f1) == File.mtime(f2)
end
def parse_options
usage if ARGV[0] == nil || ARGV[1] == nil
nodir ARGV[0] if File.directory?(ARGV[0]) == false
nodir ARGV[1] if File.directory?(ARGV[1]) == false
return ARGV
end
def datedir(date)
sprintf "%02d/%02d/%02d", date.year, date.month, date.day
end
# incomplete substitute for cp -p
def copy(src, dest)
atime = File.atime(src)
mtime = File.mtime(src)
FileUtils.copy src, dest
File.utime(atime, mtime, dest)
end
def sync_day_by_day(src, dest)
Find.find(src) do |s| # path of the source file
next unless File.file? s
r = s.sub(/^#{src}\//, "") # relative path
d = File.join dest, datedir(File.mtime(s))
f = File.join d, r
unless File.file?(f) && same_file?(s, f)
printf "%s -> %s\n", s, f
FileUtils.mkpath d unless File.directory? d
FileUtils.copy s, f
end
end
end
def main
src, dest = parse_options
sync_day_by_day src, dest
end
main