常青树 发表于 2018-8-3 06:56:19

有关puppet agent端三种备份恢复方案探讨研究

  有关puppetagent端备份恢复方案探讨:
  备份方案一、通过自定义facter结合元素backup进行备份恢复
  一、facter部署
  1、创建目录结构
  # mkdirpublic/{modules,manifests,files,lib/facter} -p
  2、打开模块中的插件功能
  # vim/etc/puppet/puppet.conf
  
  pluginsync = true
  3、编写自定义fact
  # vim/etc/puppet/modules/public/lib/facter/backup_date.rb
  # backup_date.rb
  #
  Facter.add("backup_date") do
  setcode do
  Facter::Util::Resolution.exec('/bin/date +%Y%m%d%H%M%S')
  end
  end
  4、建立环境变量(测试用)
  # exportFACTERLIB=/etc/puppet/modules/public/lib/facter
  5、测试fact(如果不正常,会显示调试信息)
  # facterbackup_date
  201307241552
  6、客户端查看facter是否被下载生效
  notice: Starting Puppet client version2.7.21
  info: Retrieving plugin
  notice:/File/ensure: defined content as'{md5}91d97be10a35ab7971f77a2be9696031'
  info: Loading downloaded plugin/var/lib/puppet/lib/facter/backup_date.rb
  info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
  info: Caching catalog foragent1.bsgchina.com
  info: Applying configuration version'1374652447'
  notice: Finished catalog run in 1.47seconds
  info: Retrieving plugin
  info: Loading facts in/var/lib/puppet/lib/facter/backup_date.rb
  info: Caching catalog foragent1.bsgchina.com
  info: Applying configuration version'1374652447'
  notice: Finished catalog run in 1.36seconds
  # ll/var/lib/puppet/lib/facter/
  total 8
  -rw-r--r-- 1 root root 138 Jul 24 16:13backup_date.rb
  #
  二、使用backup调用自定义变量
  1、在模块的config.pp文件中添加元素backup
  # vim/etc/puppet/modules/ssh/manifests/config.pp
  class ssh::config{
  file { $ssh::params::ssh_service_config:
  ensure => present,
  owner => 'root',
  group => 'root',
  mode => 0440,
  source =>"puppet:///modules/ssh/etc/ssh/sshd_config",
  backup =>".$backup_date.bak",\\添加信息
  require =>Class["ssh::install"],
  notify =>Class["ssh::service"],
  }
  }
  2、在节点查看对应目录下是否有按日期备份的文件
  # ll sshd*
  -rw-r----- 1 root root 3134 Jul 24 16:20sshd_config
  -r--r----- 1 root root 3190 Jul 24 16:17sshd_config.20130724162024.bak
  -r--r----- 1 root root 3173 Jul 24 16:20sshd_config.20130724162039.bak
  方案二、通过filebucket实现备份的集中化管理,通过节点或者puppetserver进行恢复
  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 foragent3.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 ofService
  notice:/Stage/Mysql::Service/Service: Triggered 'refresh' from 1 events
  notice: Finished catalog run in 4.34seconds
  结果:可以看到my.cnf被修改之前的版本MD5为fef73d96a75424c782191962f5aaf8ee
  5、查看设置的远程服务器端是否正常保存
  # ll/var/lib/puppet/bucket/#默认保存路径
  total 12
  drwxrwx---. 4 puppet puppet 4096 Jul 2417:56 0
  drwxrwx---. 3 puppet puppet 4096 Jul 2417:46 e
  drwxrwx---. 3 puppet puppet 4096 Jul 2417:48 f
  # tree f/
  f/
  └── e
  └── f
  └── 7
  └── 3
  └── d
  └── 9
  └── 6
  └──fef73d96a75424c782191962f5aaf8ee
  ├── contents
  └── paths
  8 directories, 2 files
  结果:保存成功,保存结果为以上目录结构
  6、只恢复某一个节点到上一个版本
  # puppet filebucketrestore /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: contentchanged '{md5}fef73d96a75424c782191962f5aaf8ee'to '{md5}09fb95f5505056b5a40c4905af3d636e'
  info: /File: Schedulingrefresh of Class
  info: Class: Schedulingrefresh of Service
  notice:/Stage/Mysql::Service/Service: Triggered 'refresh' from 1 events
  notice: Finished catalog run in 3.65seconds
  结果:可正常恢复到上一个版本(由于我这里设置了5秒钟同步puppetserver端,可以看到以上my.cnf被修改过,而且MD5值与上一版本吻合)
  8、恢复所有节点到上一个版本
  # puppet filebucketrestore --local/etc/puppet/modules/mysql/files/etc/my.cnffef73d96a75424c782191962f5aaf8ee
  9、通过调试模式查看节点动态信息
  # puppet agent--server=puppetserver.bsgchina.com --verbose --no-daemonize
  notice: Starting Puppet client version2.7.21
  info: Retrieving plugin
  info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
  info: Caching catalog foragent1.bsgchina.com
  info: Applying configuration version'1374659257'
  info: /File: Filebucketed/etc/my.cnf to main with sum 09fb95f5505056b5a40c4905af3d636e
  notice: /File/content: contentchanged '{md5}09fb95f5505056b5a40c4905af3d636e' to '{md5}fef73d96a75424c782191962f5aaf8ee'
  info: /File: Schedulingrefresh of Class
  info: Class: Schedulingrefresh of Service
  结果:节点配置文件的MD5值更新为上一个版本的MD5值,恢复成功。
  备份方案三、通过本地MD5文件进行备份恢复
  # ll/var/lib/puppet/clientbucket/
  total 40
  drwxrwx--- 3 root root 4096 Jul 24 10:51 3
  drwxrwx--- 3 root root 4096 Jul 22 14:55 7
  drwxrwx--- 3 root root 4096 Jul 24 15:31 8
  drwxrwx--- 4 root root 4096 Jul 24 10:52 e
  drwxrwx--- 3 root root 4096 Jul 22 15:10 f
  备份方案三为备份方案二的一部分,实验过程略。
  新开自动化运维管理群:296934942欢迎各界大牛加入探讨!
页: [1]
查看完整版本: 有关puppet agent端三种备份恢复方案探讨研究