PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/backup/database/mongodb.rb

https://github.com/jeffhos/backup
Ruby | 137 lines | 67 code | 23 blank | 47 comment | 3 complexity | edada34c9997876795c2f033ec399d3e MD5 | raw file
  1. # encoding: utf-8
  2. module Backup
  3. module Database
  4. class MongoDB < Base
  5. ##
  6. # Name of the database that needs to get dumped
  7. attr_accessor :name
  8. ##
  9. # Credentials for the specified database
  10. attr_accessor :username, :password
  11. ##
  12. # Connectivity options
  13. attr_accessor :host, :port
  14. ##
  15. # IPv6 support (disabled by default)
  16. attr_accessor :ipv6
  17. ##
  18. # Collections to dump, collections that aren't specified won't get dumped
  19. attr_accessor :only_collections
  20. ##
  21. # Additional "mongodump" options
  22. attr_accessor :additional_options
  23. ##
  24. # Creates a new instance of the MongoDB database object
  25. def initialize(&block)
  26. load_defaults!
  27. @only_collections ||= Array.new
  28. @additional_options ||= Array.new
  29. @ipv6 ||= false
  30. instance_eval(&block)
  31. prepare!
  32. end
  33. ##
  34. # Builds the MongoDB credentials syntax to authenticate the user
  35. # to perform the database dumping process
  36. def credential_options
  37. %w[username password].map do |option|
  38. next if send(option).nil? or send(option).empty?
  39. "--#{option}='#{send(option)}'"
  40. end.compact.join("\s")
  41. end
  42. ##
  43. # Builds the MongoDB connectivity options syntax to connect the user
  44. # to perform the database dumping process
  45. def connectivity_options
  46. %w[host port].map do |option|
  47. next if send(option).nil? or (send(option).respond_to?(:empty?) and send(option).empty?)
  48. "--#{option}='#{send(option)}'"
  49. end.compact.join("\s")
  50. end
  51. ##
  52. # Builds a MongoDB compatible string for the
  53. # additional options specified by the user
  54. def additional_options
  55. @additional_options.join("\s")
  56. end
  57. ##
  58. # Returns an array of collections to dump
  59. def collections_to_dump
  60. @only_collections
  61. end
  62. ##
  63. # Returns the MongoDB database selector syntax
  64. def database
  65. "--db='#{ name }'"
  66. end
  67. ##
  68. # Returns the mongodump syntax for enabling ipv6
  69. def ipv6
  70. @ipv6.eql?(true) ? '--ipv6' : ''
  71. end
  72. ##
  73. # Returns the MongoDB syntax for determining where to output all the database dumps,
  74. # e.g. ~/Backup/.tmp/MongoDB/<databases here>/<database collections>
  75. def dump_directory
  76. "--out='#{ dump_path }'"
  77. end
  78. ##
  79. # Builds the full mongodump string based on all attributes
  80. def mongodump
  81. "#{ utility(:mongodump) } #{ database } #{ credential_options } " +
  82. "#{ connectivity_options } #{ ipv6 } #{ additional_options } #{ dump_directory }"
  83. end
  84. ##
  85. # Performs the mongodump command and outputs the data to the
  86. # specified path based on the 'trigger'. If the user hasn't specified any
  87. # specific collections to dump, it'll dump everything. If the user has specified
  88. # collections to dump, it'll loop through the array of collections and invoke the
  89. # 'mongodump' command once per collection
  90. def perform!
  91. log!
  92. if collections_to_dump.is_a?(Array) and not collections_to_dump.empty?
  93. specific_collection_dump!
  94. else
  95. dump!
  96. end
  97. end
  98. ##
  99. # Builds and runs the mongodump command
  100. def dump!
  101. run(mongodump)
  102. end
  103. ##
  104. # For each collection in the @only_collections array, it'll
  105. # build the whole 'mongodump' command, append the '--collection' option,
  106. # and run the command built command
  107. def specific_collection_dump!
  108. collections_to_dump.each do |collection|
  109. run("#{mongodump} --collection='#{collection}'")
  110. end
  111. end
  112. end
  113. end
  114. end