Revision ab63767e

View differences:

src/onedb/onedb
64 64
    :format => String
65 65
}
66 66

  
67
FEDERATED = {
68
    :name        => "federated",
69
    :large       => "--federated",
70
    :description => "Limit backup/restore to federated tables"
71
}
72

  
67 73
###############################################################################
68 74
# SQLite options
69 75
###############################################################################
......
239 245
        Dumps the DB to a file specified in the argument
240 246
    EOT
241 247

  
242
    command :backup, backup_desc, [:output_file, nil], :options=>FORCE do
248
    command :backup, backup_desc, [:output_file, nil],
249
            :options=>[FORCE, FEDERATED] do
250

  
243 251
        begin
244 252
            helper = OneDB.new(options)
245 253
            helper.backup(args[0], options)
......
289 297
        from the same backend (SQLite or MySQL)
290 298
    EOT
291 299

  
292
    command :restore , restore_desc, [:backup_file, nil], :options=>FORCE do
300
    command :restore , restore_desc, [:backup_file, nil],
301
            :options=>[FORCE, FEDERATED] do
302

  
293 303
        begin
294 304
            helper = OneDB.new(options)
295 305
            helper.restore(args[0], options)
......
332 342
        end
333 343
    end
334 344

  
335

  
336 345
    ###########################################################################
337 346
    # Import slave
338 347
    ###########################################################################
src/onedb/onedb.rb
71 71
    end
72 72

  
73 73
    def backup(bck_file, ops, backend=@backend)
74
        bck_file = backend.bck_file if bck_file.nil?
74
        bck_file = backend.bck_file(ops[:federated]) if bck_file.nil?
75 75

  
76 76
        if !ops[:force] && File.exists?(bck_file)
77 77
            raise "File #{bck_file} exists, backup aborted. Use -f " <<
78 78
                  "to overwrite."
79 79
        end
80 80

  
81
        backend.backup(bck_file)
81
        backend.backup(bck_file, ops[:federated])
82 82
        return 0
83 83
    end
84 84

  
......
89 89

  
90 90
        one_not_running
91 91

  
92
        backend.restore(bck_file, ops[:force])
92
        backend.restore(bck_file, ops[:force], ops[:federated])
93 93
        return 0
94 94
    end
95 95

  
src/onedb/onedb_backend.rb
18 18
require 'rubygems'
19 19
require 'cgi'
20 20
require 'database_schema'
21
require 'open3'
21 22

  
22 23
begin
23 24
    require 'sequel'
......
28 29
end
29 30

  
30 31
class OneDBBacKEnd
32
    FEDERATED_TABLES = %w(group_pool user_pool acl zone_pool vdc_pool
33
                          marketplace_pool marketplaceapp_pool fed_logdb)
34

  
31 35
    def read_db_version
32 36
        connect_db
33 37

  
......
231 235
        @db_name = @db_name[1..-2] if @db_name[0] == ?"
232 236
    end
233 237

  
234
    def bck_file
238
    def bck_file(federated = false)
235 239
        t = Time.now
236
        "#{VAR_LOCATION}/mysql_#{@server}_#{@db_name}_"<<
237
        "#{t.year}-#{t.month}-#{t.day}_#{t.hour}:#{t.min}:#{t.sec}.sql"
240

  
241
        bck_name = "#{VAR_LOCATION}/mysql_#{@server}_#{@db_name}_"
242

  
243
        bck_name << "federated_" if federated
244

  
245
        bck_name << "#{t.year}-#{t.month}-#{t.day}_"
246
        bck_name << "#{t.hour}:#{t.min}:#{t.sec}.sql"
247

  
248
        bck_name
238 249
    end
239 250

  
240
    def backup(bck_file)
241
        cmd = "mysqldump -u #{@user} -p'#{@passwd}' -h #{@server} " +
242
              "-P #{@port} --add-drop-table #{@db_name} > #{bck_file}"
251
    def backup(bck_file, federated = false)
252
        cmd = "mysqldump -u #{@user} -p'#{@passwd}' -h #{@server} " <<
253
              "-P #{@port} --add-drop-table #{@db_name} "
254

  
255
        cmd << FEDERATED_TABLES.join(" ") if federated
256

  
257
        cmd << " > #{bck_file}"
243 258

  
244 259
        rc = system(cmd)
260

  
245 261
        if !rc
246 262
            raise "Unknown error running '#{cmd}'"
247 263
        end
......
252 268
        puts
253 269
    end
254 270

  
255
    def restore(bck_file, force=nil)
271
    def restore(bck_file, force=nil, federated=false)
256 272
        connect_db
257 273

  
258
        if !force && db_exists?
274
        if !federated && !force && db_exists?
259 275
            raise "MySQL database #{@db_name} at #{@server} exists," <<
260 276
                  " use -f to overwrite."
261 277
        end
......
294 310
        end
295 311
    end
296 312

  
297
    def bck_file
313
    def bck_file(federated = false)
298 314
        t = Time.now
299
        "#{VAR_LOCATION}/one.db_"<<
300
        "#{t.year}-#{t.month}-#{t.day}_#{t.hour}:#{t.min}:#{t.sec}.bck"
315
        bck_name = "#{VAR_LOCATION}/one.db_"
316

  
317
        bck_name << "federated_" if federated
318

  
319
        bck_name << "#{t.year}-#{t.month}-#{t.day}"
320
        bck_name << "_#{t.hour}:#{t.min}:#{t.sec}.bck"
321

  
322
        bck_name
301 323
    end
302 324

  
303
    def backup(bck_file)
304
        FileUtils.cp(@sqlite_file, "#{bck_file}")
305
        puts "Sqlite database backup stored in #{bck_file}"
306
        puts "Use 'onedb restore' or copy the file back to restore the DB."
307
        puts
325
    def backup(bck_file, federated = false)
326
        if federated
327
            puts "Sqlite database backup of federated tables stored in #{bck_file}"
328

  
329
            File.open(bck_file, "w") do |f|
330
                f.puts "-- FEDERATED"
331
                FEDERATED_TABLES.each do |table|
332
                    f.puts "DROP TABLE IF EXISTS \"#{table}\";"
333
                end
334
            end
335

  
336
            FEDERATED_TABLES.each do |table|
337
                Open3.popen3("sqlite3 #{@sqlite_file} '.dump #{table}' >> #{bck_file}") do |i,o,e,t|
338
                    stdout = o.read
339
                    if !stdout.empty?
340
                        puts stdout
341
                    end
342

  
343
                    stderr = e.read
344
                    if !stderr.empty?
345
                        stderr.lines.each do |line|
346
                            STDERR.puts line unless line.match(/^-- Loading/)
347
                        end
348
                    end
349
                end
350

  
351
            end
352
        else
353
            puts "Sqlite database backup stored in #{bck_file}"
354
            system("sqlite3 #{@sqlite_file} .dump > #{bck_file}")
355
        end
356

  
357
        puts "Use 'onedb restore' to restore the DB."
308 358
    end
309 359

  
310
    def restore(bck_file, force=nil)
311
        if File.exists?(@sqlite_file) && !force
312
            raise "File #{@sqlite_file} exists, use -f to overwrite."
360
    def restore(bck_file, force=nil, federated=false)
361
        if !federated
362
            if File.exists?(@sqlite_file) && !force
363
                raise "File #{@sqlite_file} exists, use -f to overwrite."
364
            end
313 365
        end
314 366

  
315
        FileUtils.cp(bck_file, @sqlite_file)
367
        system("sqlite3 #{@sqlite_file} < #{bck_file}")
316 368
        puts "Sqlite database backup restored in #{@sqlite_file}"
317 369
    end
318 370

  

Also available in: Unified diff