您的位置:首页 > 运维架构 > Linux

linux下利用ruby做系统备份与还原

2014-06-23 15:38 387 查看
啥都不说了,都在代码里 :)

#!/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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: