forked from backup/backup
/
base.rb
80 lines (63 loc) · 1.99 KB
/
base.rb
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
80
# encoding: utf-8
module Backup
module Syncer
module SCM
class Base < Syncer::Base
##
# In a server url such as:
# https://jimmy:password@example.com:80
# -> [protocol]://[username]:[password][ip]:[port]
attr_accessor :protocol, :username, :password, :ip, :port
##
# repositories is an alias to directories; provided just for clarity
alias :repositories :directories
alias :repositories= :directories=
##
# Instantiates a new Repository Syncer object
# and sets the default configuration
def initialize
load_defaults!
@path ||= 'backups'
@directories = Array.new
end
def perform!
Logger.message("#{ self.class } started syncing '#{ path }'.")
repositories.each do |repository|
backup_repository! repository
end
end
def authority
"#{protocol}://#{credentials}#{ip}#{prefixed_port}"
end
def repository_urls
repositories.collect{ |r| repository_url(r) }
end
def repository_url(repository)
"#{authority}#{repository}"
end
def repository_local_path(repository)
File.join(path, repository)
end
def repository_absolute_local_path(repository)
File.absolute_path(repository_local_path(repository))
end
def repository_local_container_path(repository)
File.dirname(repository_local_path(repository))
end
def create_repository_local_container!(repository)
FileUtils.mkdir_p(repository_local_container_path(repository))
end
private
def credentials
"#{username}#{prefixed_password}@" if username
end
def prefixed_password
":#{password}" if password
end
def prefixed_port
":#{port}" if port
end
end
end
end
end