Skip to content

Instantly share code, notes, and snippets.

@ctgswallow
Created March 1, 2013 00:16
Show Gist options
  • Save ctgswallow/5061366 to your computer and use it in GitHub Desktop.
Save ctgswallow/5061366 to your computer and use it in GitHub Desktop.
Capistrano task to create a read-only user for Drupal DB Slaves
desc "create read-only user and set access to #{db_name} database"
task :create_ro_user, :roles => :dbmaster do
# General select-only access to all tables in #{db_name}
stmt = <<-SQL
GRANT SELECT ON #{db_name}.* TO #{rouser}@'%' IDENTIFIED BY '#{ropw}';
SQL
run "mysql -u#{dbmasterroot} -p#{dbmasterrootpw} -e \"#{stmt}\""
# Look up tables to which rw grants apply
ignore_tables.each do |i|
stmt = %Q(SHOW TABLES LIKE '#{i}%';)
rwtables = capture("mysql -s -N -u#{dbmasterroot} -p#{dbmasterrootpw} -e \"#{stmt}\" #{db_name}").split("\r\n")
# Full privileges on all tables found
rwtables.each do |t|
stmt = %Q(GRANT ALL PRIVILEGES ON #{db_name}.#{t} TO #{rouser}@'%';)
run "mysql -u#{dbmasterroot} -p#{dbmasterrootpw} -e \"#{stmt}\""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment