Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "fileutils"
require "pathname"
require "date"
require "rubygems"
require "zipruby"
BACKUP_FOLDER = Pathname.new("/path/to/backup")
timestamp = DateTime.now.strftime("%Y%m%d%H%M%S")
folder = BACKUP_FOLDER + Date.today.strftime("%Y%m%d")
FileUtils.mkdir(folder) unless File.exist?(folder)
ARGV.each do |path|
Zip::Archive.open((folder + (File.basename(path) + "-" + timestamp)).to_s + ".zip", Zip::CREATE) do |zip|
target = Pathname.new(path)
case File::ftype(target)
when "file"
zip.add_file(target.to_s)
when "directory"
Dir[target + "**/**"].each do |file|
file_path = file.sub(File.dirname(target), "")
case File::ftype(file)
when "file"
zip.add_file(file_path, file)
when "directory"
zip.add_dir(file_path)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment