From 3c384653e4eca65fa973af9bd611bb09cff6900b Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Wed, 30 Mar 2011 17:03:43 +0200 Subject: [PATCH] Continue developing. --- LICENSE.TXT | 19 ++++++++ bin/phusion-backup | 88 ++++++++++++++++++++++++++++++++++++- phusion-backup.gemspec | 20 +++++++++ resources/default-files.txt | 1 + 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 LICENSE.TXT create mode 100644 phusion-backup.gemspec diff --git a/LICENSE.TXT b/LICENSE.TXT new file mode 100644 index 0000000..e0d0bb4 --- /dev/null +++ b/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. \ No newline at end of file diff --git a/bin/phusion-backup b/bin/phusion-backup index 41771d6..a80ac5d 100755 --- a/bin/phusion-backup +++ b/bin/phusion-backup @@ -1,4 +1,8 @@ #!/usr/bin/env ruby +# encoding: binary +require 'etc' +require 'optparse' + module PhusionBackup module TUI @@ -29,6 +33,7 @@ protected end def substitute_color_tags(data) + data = data.to_s data = data.gsub(%r{(.*?)}m, "\e[1m\\1#{DEFAULT_TERMINAL_COLORS}") data.gsub!(%r{(.*?)}m, "\e[1m\e[31m\\1#{DEFAULT_TERMINAL_COLORS}") data.gsub!(%r{(.*?)}m, "\e[1m\e[32m\\1#{DEFAULT_TERMINAL_COLORS}") @@ -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}" @@ -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 "No `files.txt` found in this backup directory." @@ -116,6 +127,17 @@ private end end + def copy_file_no_overwrite(source, target) + puts "[Generating] #{target}" + if File.exist?(target) + puts "File already exists. Not overwritten." + 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| @@ -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 " + 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 @@ -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 diff --git a/phusion-backup.gemspec b/phusion-backup.gemspec new file mode 100644 index 0000000..a4a849f --- /dev/null +++ b/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 + diff --git a/resources/default-files.txt b/resources/default-files.txt index 2523218..befe0fc 100644 --- a/resources/default-files.txt +++ b/resources/default-files.txt @@ -30,3 +30,4 @@ # Phusion stuff. /etc/init.d/firewall +/opt/production/nginx/conf