閱讀593 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

  上一篇:go Threads and Anonymous Classes in JAVA
  下一篇:go Myeclipse重裝後的必要配置