-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathftp.rb
More file actions
131 lines (111 loc) · 3.06 KB
/
ftp.rb
File metadata and controls
131 lines (111 loc) · 3.06 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'net/ftp'
require 'optparse'
require 'fileutils'
class FTPOperationsParser
def self.validate(options)
if not options[:userid] or not options[:password]
$stderr.puts "must provide options userid and password"
return false
end
return true
end
def self.usage_notes
script_name=$0
$stderr.puts <<END
Example :
#{script_name} -u <username> -p <password> --listdir #lists all directory on ftp server
#{script_name} -u <username> -p <password> --dir <ftpdir> --backupdir <local_backup_dir> #backup all files from ftp server-dir to local dir
END
end
def self.parse(args)
options= {}
opts=OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] "
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("-u", "--userid USERID", String, "ftp user id") do |v|
options[:userid] = v
end
opts.on("-p", "--password PASSWORD", String, "ftp password password") do |v|
options[:password] = v
end
opts.on("-l", "--dir DIRNAME", String, "ftp dir") do |v|
options[:dir] = v
end
opts.on("--listdir", "list all ftp directories") do |v|
options[:listdir] = v
end
opts.on("--backupdir BACKUPDIR", String, "local backup dir") do |v|
options[:backupdir] = v
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
usage_notes
exit(-1)
end
end
opts.parse!(args)
if validate(options)
return options
else
$stderr.puts "Invalid/no args, see use -h command for help"
exit(-1)
end
end
end
options=FTPOperationsParser.parse(ARGV)
class FTPOperations
def ftp_login(options)
ftp=nil
begin
ftp = Net::FTP.new
ftp.connect("127.0.0.1")
ftp.login(options[:userid], options[:password])
rescue Exception=>e
$stderr.puts "#{e.class}:#{e.message}"
end
return ftp
end
def list_dir(options)
begin
raise Exception.new("ftp can't be nil") if not options[:ftp]
puts options[:ftp].list
rescue Exception=>e
$stderr.puts "exception while listing label:#{e.class}:#{e.message}"
end
end
def bkp_files(options)
begin
raise Exception.new("ftp can't be nil") if not options[:ftp]
raise Exception.new("ftp dir not provided") if not options[:dir]
raise Exception.new("local backup dir not provided") if not options[:backupdir]
backup_dir = File.join(options[:backupdir], options[:dir])
if not File.directory?(backup_dir)
FileUtils.mkdir_p(backup_dir)
end
options[:ftp].chdir(options[:dir])
options[:ftp].nlst.each do |file|
begin
options[:ftp].getbinaryfile(file, File.join(backup_dir, file))
rescue Net::FTPPermError=> e
$stderr.puts "skipping dir:#{file}"
end
end
rescue Exception=> e
$stderr.puts "exception while copying ftp file to local dir:#{e.class}:#{e.message}"
end
end
end
ftp_opn_obj = FTPOperations.new
ftp = ftp_opn_obj.ftp_login(options)
if not ftp
$stderr.puts "could not connect to ftp server."
exit 1
end
options[:ftp]=ftp
if options[:listdir]
ftp_opn_obj.list_dir(options)
elsif options[:backupdir] and options[:dir]
ftp_opn_obj.bkp_files(options)
end