2e232 发表于 2014-4-21 09:25:24

puppet通过filebucket实现生产节点文件的恢复介绍

场景:在一次通过puppet执行变更后,某一个节点由于特殊性需要恢复到上一次被覆盖的配置文件,puppet是如何做到的。
puppet节点会在每一次更新之前,将当前运行环境的配置文件以MD5值生成路径保存到默认目录/var/lib/puppet/bucket/,里面包含了每个配置文件的内容、文件路径和名称,通过filebucket命令指定文件的MD5值便可以恢复。当然也可以将保存的文件内容复制到当前环境对应的配置文件中进行修复。
PS: 如果要全部恢复到上一个版本,可结合SVN或git进行操作。
下面是具体的实施步骤:
1、在site.pp中添加filebucket资源
# vim /etc/puppet/manifests/site.pp   
import 'nodes/*'
$puppetserver = 'puppetserver.bsgchina.com'   
filebucket { 'main':
path   => false,#设置agent节点本地不需要保存
# path   => "/var/lib/puppet/databackup",
server => 'puppetserver.bsgchina.com'   #设置将文件更改过之前的版本保存到远程服务器puppetserver.bsgchina.com上
}
2、在puppetmaster上修改模块配置文件
# vim /etc/puppet/modules/mysql/manifests/config.pp
class mysql::config{
file { "/etc/my.cnf":
    ensure=> present,
    owner   => 'mysql',
    group   => 'mysql',
    mode    => 0644,
    source=> "puppet:///modules/mysql/etc/my.cnf",
    backup=> 'main',#设置backup备份方式为之前site.pp中定义的main方式
#   backup=> ".$backup_date.bak",
    require => Class["mysql::install"],
    notify=> Class["mysql::service"],
}

file { "/var/lib/mysql":
    group   => 'mysql',
    owner   => 'mysql',
    recurse => 'true',
    require => File["/etc/my.cnf"],
}
}
3、修改测试文件模拟新版本发布
vim /etc/puppet/modules/mysql/files/etc/my.cnf
4、节点进行监听
# puppet agent --server=puppetserver.bsgchina.com --verbose --no-daemonize
info: Retrieving plugin
info: Loading facts in backup_date
info: Loading facts in backup_date
info: Caching catalog for agent3.bsgchina.com
info: Applying configuration version '1374659257'
info: /Stage/Mysql::Config/File: Filebucketed /etc/my.cnf to main with sum fef73d96a75424c782191962f5aaf8ee
notice: /Stage/Mysql::Config/File/content: content changed '{md5}fef73d96a75424c782191962f5aaf8ee' to '{md5}09fb95f5505056b5a40c4905af3d636e'
info: /Stage/Mysql::Config/File: Scheduling refresh of Service
notice: /Stage/Mysql::Service/Service: Triggered 'refresh' from 1 events
notice: Finished catalog run in 4.34 seconds
结果:可以看到my.cnf被修改之前的版本MD5为fef73d96a75424c782191962f5aaf8ee
5、查看设置的远程服务器端是否正常保存
# ll /var/lib/puppet/bucket/#默认保存路径
total 12
drwxrwx---. 4 puppet puppet 4096 Jul 24 17:56 0
drwxrwx---. 3 puppet puppet 4096 Jul 24 17:46 e
drwxrwx---. 3 puppet puppet 4096 Jul 24 17:48 f
# tree f/
f/
└── e
    └── f
      └── 7
            └── 3
                └── d
                  └── 9
                        └── 6
                            └── fef73d96a75424c782191962f5aaf8ee
                              ├── contents
                              └── paths
8 directories, 2 files
结果:保存成功,保存结果为以上目录结构
6、只恢复某一个节点到上一个版本
# puppet filebucket restore /etc/my.cnffef73d96a75424c782191962f5aaf8ee#节点上操作
7、通过调试模式查看节点动态信息
# puppet agent --server=puppetserver.bsgchina.com --verbose --no-daemonize
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
info: Caching catalog for agent1.bsgchina.com
info: Applying configuration version '1374659257'
info: /File: Filebucketed /etc/my.cnf to main with sum fef73d96a75424c782191962f5aaf8ee
notice: /File/content: content changed '{md5}fef73d96a75424c782191962f5aaf8ee' to '{md5}09fb95f5505056b5a40c4905af3d636e'
info: /File: Scheduling refresh of Class
info: Class: Scheduling refresh of Service
notice: /Stage/Mysql::Service/Service: Triggered 'refresh' from 1 events
notice: Finished catalog run in 3.65 seconds
结果:可正常恢复到上一个版本(由于我这里设置了5秒钟同步puppetserver端,可以看到以上my.cnf被修改过,而且MD5值与上一版本吻合)
8、恢复所有节点到上一个版本
# puppet filebucket restore --local/etc/puppet/modules/mysql/files/etc/my.cnffef73d96a75424c782191962f5aaf8ee
9、通过调试模式查看节点动态信息
# puppet agent --server=puppetserver.bsgchina.com --verbose --no-daemonize
notice: Starting Puppet client version 2.7.21
info: Retrieving plugin
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
info: Caching catalog for agent1.bsgchina.com
info: Applying configuration version '1374659257'
info: /File: Filebucketed /etc/my.cnf to main with sum 09fb95f5505056b5a40c4905af3d636e
notice: /File/content: content changed '{md5}09fb95f5505056b5a40c4905af3d636e' to '{md5}fef73d96a75424c782191962f5aaf8ee'
info: /File: Scheduling refresh of Class
info: Class: Scheduling refresh of Service
结果:节点配置文件的MD5值更新为上一个版本的MD5值,恢复成功。

页: [1]
查看完整版本: puppet通过filebucket实现生产节点文件的恢复介绍