Skip to content
Extraits de code Groupes Projets
backup.rake 1,88 ko
Newer Older
  • Learn to ignore specific revisions
  • namespace :backup do
      desc "Backup Mongo"
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
      require File.join(Rails.root, 'config', 'initializers', '_load_app_config.rb')
      require 'cloudfiles'
    
    
      task :mongo do
    
        puts("event=backup status=start type=mongo")
    
        if AppConfig[:cloudfiles_username] && AppConfig[:cloudfiles_api_key]
    
    danielvincent's avatar
    danielvincent a validé
          puts "Logging into Cloud Files"
    
    
          cf = CloudFiles::Connection.new(:username => AppConfig[:cloudfiles_username], :api_key => AppConfig[:cloudfiles_api_key])
    
          mongo_container = cf.container("Mongo Backup")
    
          puts "Dumping Mongo"
    
          `mongodump -o /tmp/backup/mongo`
    
          tar_name = "mongo_#{Time.now.to_i}.tar"
          `tar cfP /tmp/backup/#{tar_name} /tmp/backup/mongo`
    
          file = mongo_container.create_object(tar_name)
    
    danielvincent's avatar
    danielvincent a validé
    
          if file.write File.open("/tmp/backup/" + tar_name)
    
            puts("event=backup status=success type=mongo")
    
            `rm /tmp/backup/#{tar_name}`
            `rm -rf /tmp/backup/mongo/`
    
    danielvincent's avatar
    danielvincent a validé
          else
    
            puts("event=backup status=failure type=mongo")
    
    danielvincent's avatar
    danielvincent a validé
          end
    
        else
          puts "Cloudfiles username and api key needed"
        end
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
    
      task :photos do
    
        puts("event=backup status=start type=photos")
    
        if AppConfig[:cloudfiles_username] && AppConfig[:cloudfiles_api_key]
    
    danielvincent's avatar
    danielvincent a validé
          puts "Logging into Cloud Files"
    
    
          cf = CloudFiles::Connection.new(:username => AppConfig[:cloudfiles_username], :api_key => AppConfig[:cloudfiles_api_key])
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          photo_container = cf.container("Photo Backup")
    
          tar_name = "photos_#{Time.now.to_i}.tar"
          `tar cfP /tmp/backup/#{tar_name} /usr/local/app/diaspora/public/uploads/images/`
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
          file = photo_container.create_object(tar_name)
    
    danielvincent's avatar
    danielvincent a validé
    
          if file.write File.open("/tmp/backup/" + tar_name)
    
            puts("event=backup status=success type=photos")
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
            `rm /tmp/backup/#{tar_name}`
    
    danielvincent's avatar
    danielvincent a validé
          else
    
            puts("event=backup status=failure type=photos")
    
    danielvincent's avatar
    danielvincent a validé
          end
    
    zhitomirskiyi's avatar
    zhitomirskiyi a validé
        else
          puts "Cloudfiles username and api key needed"
        end
      end