Skip to content

Instantly share code, notes, and snippets.

@srenatus
Last active August 29, 2015 14:02
Show Gist options
  • Save srenatus/51ba25526893a362c3d5 to your computer and use it in GitHub Desktop.
Save srenatus/51ba25526893a362c3d5 to your computer and use it in GitHub Desktop.
Chefdiff, executing a diff between the latest (chef) backup and the file in its usual location. Can be used with relative paths, e.g. `cd /etc; chefdiff my/files.conf`.
#!/opt/chef/embedded/bin/ruby
#
# Copyright (C) 2014 x-ion GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'date'
file = File.expand_path ARGV.last
prefix = File.join('/var/chef/backup', "#{file}.chef-")
latest_backup = Dir.glob("#{prefix}*").sort.last
exit 1 unless latest_backup
date_part = latest_backup.gsub(/#{prefix}/, '')
date = DateTime.strptime(date_part, '%Y%m%d%H%M%S')
cmd = "diff #{file} #{latest_backup}"
system cmd
puts "ran: '#{cmd}'"
puts "backup date: #{date}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment