augustash / capistrano-ash

August Ash recipes for Capistrano
http://augustash.com
MIT License
73 stars 12 forks source link

should rescue from exceptions during backup:web #47

Closed martensc closed 10 years ago

martensc commented 11 years ago

The backup:web was failing to remove the copied release directory from the tmp folder located in the backups directory. Also had to change permissions to 755 to make sure the ssh user could actually remove everything.

# overrides backup:web in ash/base.rb
namespace :backup do
  desc <<-DESC
    Requires the rsync package to be installed.

    Performs a file-level backup of the application and any assets \
    from the shared directory that have been symlinked into the \
    applications root or sub-directories.

    You can specify which files or directories to exclude from being \
    backed up (i.e., log files, sessions, cache) by setting the \
    :backup_exclude variable
    set(:backup_exclude) { [ "var/", "tmp/", logs/debug.log ] }
  DESC
  task :web, :roles => :web do
    if previous_release
      puts "Backing up web files (user uploaded content and previous release)"

      if !backup_exclude.nil? && !backup_exclude.empty?
        logger.debug "processing backup exclusions..."
        backup_exclude.each do |pattern|
          exclude_string << "--exclude '#{pattern}' "
        end
        logger.debug "Exclude string = #{exclude_string}"
      end

      # Copy the previous release to the /tmp directory
      logger.debug "Copying previous release to the #{tmp_backups_path}/#{release_name} directory"
      run "rsync -avzrtpL #{exclude_string} #{current_path}/ #{tmp_backups_path}/#{release_name}/"

      begin
        # --------------------------
        # SET/RESET PERMISSIONS
        # --------------------------
        set_perms_dirs("#{tmp_backups_path}/#{release_name}", 755)
        set_perms_files("#{tmp_backups_path}/#{release_name}", 755)

        # create the tarball of the previous release
        set :archive_name, "release_B4_#{release_name}.tar.gz"
        logger.debug "Creating a Tarball of the previous release in #{backups_path}/#{archive_name}"
        run "cd #{tmp_backups_path} && tar -cvpf - ./#{release_name}/ | gzip -c --best > #{backups_path}/#{archive_name}"

        # remove the the temporary copy
        logger.debug "Removing the tempory copy"
        run "rm -rf #{tmp_backups_path}/#{release_name}"
      rescue Exception => e
        logger.debug e.message
        logger.info "caught an excpetion but keep on truckin..."
      end
    else
      logger.important "no previous release to backup; backup of files skipped"
    end
  end
end