-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbak
executable file
·76 lines (68 loc) · 1.74 KB
/
bak
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
#!/usr/bin/env ruby
# coding: utf-8
# desc: backup files according to a yaml file
# author: eric
require 'yaml'
BACKUP_DIR_PREFIX = "#{Dir.home}/.sync/Seafile/"
DATA_PATH = "#{Dir.home}/.backup.yaml"
def sync_exec args
rsync_opt = '--times --recursive'
file = args[:file].dup
dest = args[:dest].dup
if args[:relative] == true
rsync_opt << " --relative"
file.sub!(args[:strip], "") if args[:strip]
end
cmd = %[rsync #{rsync_opt} '#{file}' '#{dest}']
# pass "test" as command parameter to dry-run.
unless ARGV.include? "test"
if system cmd
print "[" + "\e[32m" + "done" + "\e[0m" + "]" # done: green
else
print "[" + "\e[31m" + "failed" + "\e[0m" + "]" # failed: red
end
puts args[:file]
else
puts cmd
end
end
def sync list
dest = BACKUP_DIR_PREFIX + list['backup_dir']
# relative copy
if list['relative_copy'] == true
# if need to strip prefix
if list['strip_prefix']
Dir.chdir list['strip_prefix'] do
list['files'].each do |file|
sync_exec file: file,
dest: dest,
relative: true,
strip: list['strip_prefix']
end
end
# standard relative copy
else
list['files'].each do |file|
sync_exec file: file,
dest: dest,
relative: true
end
end
# normal copy
else
list['files'].each do |file|
sync_exec file: file, dest: dest
end
end
end
# main
data = File.open(DATA_PATH) { |f| YAML.load f }
data.each do |list|
if ARGV.include? "test"
print "\e[32m"
print list['strip_prefix'] + ' ' if list['strip_prefix']
print "=> #{BACKUP_DIR_PREFIX}#{list['backup_dir']}"
print "\e[0m" + "\n"
end
sync list
end