Cool Dba

  • Home
  • Sql tricks
  • Security
  • Maintainance
  • Backups
  • Recovery
  • Replication
  • Installation
  • Linux
Showing posts with label Backups. Show all posts
Showing posts with label Backups. Show all posts

Thursday, May 26, 2016

Simple bash script's for managing mysql

 Anil Joshi     10:09 PM     Backups, Database Maintainance, Replication, Scripts     No comments   



1. Monitoring replication health

#!/bin/bash

sql_thread_running=$(mysql -e "show slave status\G" | awk -F":" '/Slave_SQL_Running/ { print $2 }' | tr -d ' ')
io_thread_running=$(mysql -e "show slave status\G" | awk -F":" '/Slave_IO_Running/ { print $2 }' | tr -d ' ')
seconds_late=$(mysql -e "show slave status\G" | awk -F":" '/Seconds_Behind_Master/ { print $2 }' | tr -d ' ')
seconds_late=$(($seconds_late+0))

if [ "$sql_thread_running" = "No" ] || [ "$io_thread_running" = "No" ] || [ $seconds_late -gt 3600 ]||[ "$io_thread_running" = "Connecting" ]; then

mysql -e "show slave status\G"|mail -s "Slave Database Issue" xxx.gmail.com

fi

2.Monitoring mysql status

 #!/bin/bash

 /usr/bin/mysqladmin  ping| grep 'mysqld is alive' > /dev/null 2>&1
 if [ $? != 0 ]
 then
     echo 'Mysql Service is Stop'|mail -s "Slave Mysql service down" xxx@gmail.com 

  else
     echo 'Mysql Service Running'

 fi


3.Backup mysql data

#!/bin/bash

databases=`mysql -e "SHOW DATABASES;" | grep -E "(test1|test2)"`

for db in $databases; do
  mysqldump  --databases $db | gzip > /home/mysql/data/$db`date +%Y%m%d`.sql.gz;
  mysqldump  --no-data --databases $db | gzip > /home/mysql/schema/$db`date +%Y%m%d`.sql.gz;

  done

find /home/mysql/data  -iname "*.gz" -mtime +7 -exec rm {} \;
find /home/mysql/schema  -iname "*.gz" -mtime +7 -exec rm {} \;

echo 'Dump completed Successfully'|mail -s "Dump completed" xxx@gmail.com

This script will hold last 7 days backup.Change it according to your need.

Hope this article helps !

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Sunday, May 22, 2016

How to Export and Import files in Mysql

 Anil Joshi     8:14 AM     Backups, sql tricks     No comments   


 Import of csv file into mysql(Linux)

LOAD DATA INFILE '/path to csv file/' INTO TABLE 'tbl_name'
  FIELDS TERMINATED BY ',' ENCLOSED BY '"'
  LINES TERMINATED BY '\r\n'
  IGNORE 1 LINES
 (@col1,@col2) SET fileld1=@col1,field2=@col2;

 Import of csv file into mysql(Windows)

LOAD DATA LOCAL INFILE '//path to csv file//'
INTO TABLE `tbl_name`
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(`field1, `field2`, `field3`, `field4`)

Export of mysql data into csv file(Linux)

SELECT col1,col2
union
SELECT col1,col2  INTO OUTFILE '/path to csv/'
from 'tbl_name'
FIELDS TERMINATED BY ','ENCLOSED BY '"'
LINES TERMINATED BY '\n' 

Export of mysql data into csv file(Windows)

SELECT col1,col2
union
SELECT col1,col2 INTO OUTFILE '\\path to csv\\'
FROM tbl_name
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';


Find columns names from a table in case of large columns
(helpful while importing large data with column name)

select GROUP_CONCAT(CONCAT("'",COLUMN_NAME,"'"))
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tbl_name'
AND TABLE_SCHEMA = 'db_name'
order BY ORDINAL_POSITION


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Thursday, May 19, 2016

Store Procedure & Views Backups

 Anil Joshi     12:06 AM     Backups     No comments   















How to Backup Store Procedures ?


mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt <database> > outputfile.sql

mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt  mydb > mydb.sql

mysqldump  --compact --no-create-info \
    --where="db='db_name' AND type='PROCEDURE' AND name IN ('')" \
    --databases mysql --tables proc > outputfile.sql

mysql --user=root mydb -e "SHOW CREATE PROCEDURE myprocedure;" > myprocedure.sql

mysql db_name -e "show create procedure proc_name\G;">/directory/


How to Backup Views ?

mysql db_name -e "show create view view_culture\G;">/directory/

Mysqldump  db_name view_name>/directory/



Simple Bash script for taking views dump


#!/bin/bash -e
mysql --skip-column-names --batch -e \
"select table_name from information_schema.views \
 where table_schema = database()" $* |
xargs --max-args 1 mysqldump $*


save it as mysql-dump-views.sh


$ mysql-dump-views.sh -u user -ppassword databasename >dumpfile.sql

That's It.












Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Older Posts Home

Deals on Amazon

Mobiles on Amazon

About Me

My photo
Anil Joshi
New Delhi, Delhi, India
I am a simple man with little knowledge of computer software and Tehnology.
View my complete profile

Popular Posts

  • SELinux mechanism in linux environment.
    Security-Enhanced Linux (SELinux) is a mandatory access control (MAC) security mechanism implemented in the kernel. SELinux was first ...
  • Complete uninstallation of Mysql Server on centos
    Step 1: Check list the mysql rpm which is installed on server rpm -qa | grep mysql or yum list installed | grep mysql Step 2 : Re...
  • Master-Slave Replication without downtime
    First, we configure master’s   /etc/mysql/my.cnf   by including these lines in the [mysqld] section: server - id = 1 bin...
  • Dual Master Replication(Master-Master)
    MySQL replication is the most flexible way to deal with scalability and availability. The most common problem with replication is ...
  • Tracing the execution of processes using strace
    Strace is quite simply a tool that traces the execution of system calls. It can trace the execution of a binary from start to end, and ou...
  • Mysql Installation using rpm package
        1.Download the required rpm filess MySQL-client-community-5.1.25-0.rhel5.i386.rpm MySQL-server-community-5.1.25-0.rhel5.i386....
  • Filter Replication Variables
    Filtering can be done on the Master or on the Slave. Filtering on the Slave is normally preferred as in this case you get full binary lo...
  • Mysql Security
    Improved  MySQL Database Se curity 1. Secure your server Many known attacks are possible only once physical access to a machi...
  • Finding and preventing ddos attack on Linux Server
    A distributed denial-of-service (DDoS) attack occurs when multiple systems flood the bandwidth or resources of a targeted system, usual...
  • Recovering InnoDB MySQL Tables Data from ibdata and .frm Files
    First Check Error Logs. InnoDB: Error: log file ./ib_logfile0 is of different size 0 50331648 bytes InnoDB: than specified in th...
Best Shoes on amazon

Categories

  • Backups
  • Database Maintainance
  • Installation
  • Linux
  • Mysql
  • Recovery
  • Replication
  • Scripts
  • Security
  • SELinux
  • sql tricks
  • Strace

Pages

  • Home

Blog Archive

  • ▼  2016 (19)
    • ▼  June (1)
      • Complete uninstallation of Mysql Server on centos
    • ►  May (18)

Sample Text

Copyright © Cool Dba | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates