Revision ab63767e src/onedb/onedb_backend.rb

View differences:

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