daily pastebin goal
66%
SHARE
TWEET

Untitled

a guest Jun 29th, 2018 112 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. set :dbuser "user"
  2. set :dbhost "host"
  3. set :database "db"
  4.  
  5. namespace :db do
  6.   desc "Get the database password from user"
  7.   task :get_password do
  8.     set(:dbpass) do
  9.       Capistrano::CLI.ui.ask "Enter mysql password: "
  10.     end
  11.   end
  12.  
  13.   task :backup_name, :only => { :primary => true } do
  14.     now = Time.now
  15.     run "mkdir -p #{shared_path}/db_backups"
  16.     backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
  17.     set :backup_file, "#{shared_path}/db_backups/#{database}-snapshot-#{backup_time}.sql"
  18.   end
  19.  
  20.   desc "Dump database to backup file"
  21.   task :dump, :roles => :db, :only => {:primary => true} do
  22.     backup_name
  23.     run "mysqldump --add-drop-table -u #{dbuser} -h #{dbhost} -p#{dbpass} #{database} | bzip2 -c > #{backup_file}.bz2"
  24.   end
  25. end
  26.    
  27. filename = 'wp-config.php'
  28. def get_db_info(file)
  29.   username = nil
  30.   password = nil
  31.   db_name = nil
  32.  
  33.   file.each { |line|
  34.     if line =~ /'DB_(USER|PASSWORD|NAME)', '([[:alnum:]]*)'/
  35.       if $1 == "USER"
  36.         username = $2
  37.       elsif $1 == "PASSWORD"
  38.         password = $2
  39.       elsif $1 == "NAME"
  40.         db_name = $2
  41.       end
  42.     end
  43.   }
  44.  
  45.   if username.nil? || password.nil? || db_name.nil?
  46.     puts "[backup_db][bad] couldn't get all needed info"
  47.     exit
  48.   end
  49.  
  50.   return username, password, db_name
  51. end
  52.  
  53. begin
  54.   config_file = open("#{filename}")
  55. rescue Errno::ENOENT
  56.   puts "[backup_db][bad] File '#{filename}' didn't exist"
  57.   exit
  58. else
  59.   puts "[backup_db][good] File '#{filename}' existed"
  60. end
  61.  
  62. username, password, db_name = get_db_info(config_file)
  63. sql_dump_info = `mysqldump --user=#{username} --password=#{password} #{dbname}`
  64. puts sql_dump_info
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top