Skip to content

Commit

Permalink
Continue developing.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Mar 30, 2011
1 parent 06d46d4 commit 3c38465
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
19 changes: 19 additions & 0 deletions LICENSE.TXT
@@ -0,0 +1,19 @@
Copyright (c) 2011 Phusion

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
88 changes: 86 additions & 2 deletions bin/phusion-backup
@@ -1,4 +1,8 @@
#!/usr/bin/env ruby
# encoding: binary
require 'etc'
require 'optparse'

module PhusionBackup

module TUI
Expand Down Expand Up @@ -29,6 +33,7 @@ protected
end

def substitute_color_tags(data)
data = data.to_s
data = data.gsub(%r{<b>(.*?)</b>}m, "\e[1m\\1#{DEFAULT_TERMINAL_COLORS}")
data.gsub!(%r{<red>(.*?)</red>}m, "\e[1m\e[31m\\1#{DEFAULT_TERMINAL_COLORS}")
data.gsub!(%r{<green>(.*?)</green>}m, "\e[1m\e[32m\\1#{DEFAULT_TERMINAL_COLORS}")
Expand All @@ -53,6 +58,7 @@ class Core
def servers
results = []
@backupdirs.each do |dir|
next if !File.exist?(dir)
Dir.foreach(dir) do |hostname|
next if hostname =~ /^\./
fullname = "#{dir}/#{hostname}"
Expand All @@ -68,6 +74,11 @@ class Core
return servers.find { |s| s.hostname == hostname_or_dir || s.dir == hostname_or_dir }
end

def generate(dir)
copy_file_no_overwrite("#{ROOT}/resources/default-files.txt", "#{dir}/files.txt")
copy_file_no_overwrite("#{ROOT}/resources/default-install-script.txt", "#{dir}/install-script.txt")
end

def run_backup(server)
if !File.exist?("#{server.dir}/files.txt")
puts "<red>No `files.txt` found in this backup directory.</red>"
Expand Down Expand Up @@ -116,6 +127,17 @@ private
end
end

def copy_file_no_overwrite(source, target)
puts "[Generating] #{target}"
if File.exist?(target)
puts "<yellow>File already exists. Not overwritten.</yellow>"
else
File.open(target, "w") do |f|
f.write(File.read(source))
end
end
end

def create_globbing_filelist(server)
filename = "/tmp/phusion-backup-#{Process.pid}.txt"
File.open(filename, 'a') do |target|
Expand All @@ -140,11 +162,68 @@ class App
end

def run
core = Core.new(["#{Core::ROOT}/backups"])
options = {}
parser = OptionParser.new do |opts|
nl = "\n" << (" " * 37)
opts.banner = "General usage: phusion-backup <options...>"
opts.separator ""

opts.separator "Backup one or more servers:"
opts.separator " phusion-backup hostname1 [hostname2 ...]"
opts.separator ""
opts.separator " `hostnameX' is either a host name or directory name."
opts.separator ""

opts.separator "Display all servers that phusion-backup knows about:"
opts.separator " phusion-backup --list"
opts.separator ""

opts.separator "Please read the README for tutorial!"
opts.separator ""
opts.separator "Available options:"
opts.on("--list",
"Show all servers that phusion-backup knows#{nl}" +
"about.") do
options[:list] = true
end
opts.on("--generate DIR", String,
"Generate default files (like files.txt)#{nl}" +
"in the given directory.") do |dir|
options[:generate] = dir
end
opts.on("-h", "--help", "Show this help message.") do
options[:help] = true
end
end
begin
parser.parse!(@argv)
rescue OptionParser::ParseError => e
puts e
puts
puts "Please see '--help' for valid options."
exit 1
end

if options[:help]
puts parser
return 0
end

core = Core.new(["#{Core::ROOT}/backups", "#{home_dir}/Backups"])

if options[:list]
core.servers.each do |server|
printf "%-35s %s\n", server.hostname, server.dir
end
return 0
elsif options[:generate]
core.generate(options[:generate])
return 0
end

if @argv.empty?
if core.servers.empty?
puts_error "There are no servers to backup. Please read README for usage."
puts_error "You must create a backup specification first. Please read README for tutorial."
else
puts_error "Please specify the server you want to backup:"
puts
Expand Down Expand Up @@ -180,6 +259,11 @@ class App
rescue Core::CommandError
return 1
end

private
def home_dir
return Etc.getpwuid(Process.uid).dir
end
end

end
Expand Down
20 changes: 20 additions & 0 deletions phusion-backup.gemspec
@@ -0,0 +1,20 @@
Gem::Specification.new do |s|
s.name = "phusion-backup"
s.version = "1.0.0"
s.authors = ["Hongli Lai"]
s.date = "2011-03-30"
s.description = "Simple backup tool utilizing rdiff-backup."
s.summary = "Simple backup tool utilizing rdiff-backup."
s.email = "hongli@phusion.nl"
s.files = Dir[
"LICENSE.TXT",
"phusion-backup.gemspec",
"bin/*",
"resources/*"
]
s.homepage = "https://github.com/FooBarWidget/crash-watch"
s.rdoc_options = ["--charset=UTF-8"]
s.executables = ["crash-watch"]
s.require_paths = ["lib"]
end

1 change: 1 addition & 0 deletions resources/default-files.txt
Expand Up @@ -30,3 +30,4 @@

# Phusion stuff.
/etc/init.d/firewall
/opt/production/nginx/conf

0 comments on commit 3c38465

Please sign in to comment.