linux下利用ruby做系统备份与还原
啥都不说了,都在代码里 :)
#!/usr/bin/ruby
BAK_PATH = "/media/backup.tar.xz"
def to_backup
exclude_files = ""
pre_cmd = "sudo tar -cvpJf #{BAK_PATH}"
DATA.each_line do |line|
exclude_files << "--exclude=#{line.chomp} "
end
cmd = pre_cmd + " " + exclude_files + "/"
puts "#{cmd}"
print "Are you sure?[y|n]"
if STDIN.gets.match("^y$")
#puts "start ghost..."
#puts `#{cmd}` can't echo back to screen ,so use pipe
pipe = open("|#{cmd}")
pipe.each_line.each_with_index {|line,i| puts "#{i}: #{line}"}
else
puts "nothing happened...exit! :)"
end
end
def to_restore
print "Are you sure restore #{BAK_PATH} to / [y|n]"
if STDIN.gets.match("^y$")
pipe = open("|sudo tar -xvpJf #{BAK_PATH} -C /")
pipe.each_line.each_with_index {|line,i| puts "#{i}: #{line}"}
else
puts "nothing happened...exit! :)"
end
end
if ARGV[0] && ARGV[0] == "-r"
to_restore
else
to_backup
end
#exclude path ,you can add as your wise.if blank in path you need use 'path' :)
__END__
/proc
/lost+found
/mnt
/sys
/media
/home/wisy/bak
/home/wisy/下载
/home/wisy/bin
'/home/wisy/VirtualBox VMs'
最后更新:2017-04-03 07:57:12