October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Mysql Replication status notification

Using the script you can get the alert message from the replication server if replication is down or not working.

——————————————————————————————–

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

#script checking the replication is running or not.

#If replication is down then sent the alert mail.

slave_server_hostname=192.168.10.1

###check if already notified###

cd /root

if [ -f slave_problem.txt ]; then

rm -rf /root/slave_problem.txt

exit 1;

fi

 

###Check if slave running###

(

echo “show slave status \G;”

) | mysql -u username -h $slave_server_hostname -ppassword 2>&1 | grep “Slave_IO_Running: No”

if [ “$?” -ne “1” ]; then

echo “Replication Failed”

echo “Replication failed” > /root/slave_problem.txt

fi

 

###Send notification if replication down###

cd /root

if [ -f slave_problem.txt ]; then

#mail -s “Replication problem” mail_id@domainname.com< /root/slave_problem.txt

echo “Problem in replicaition”

fi

———————————————————————————————-

Setup this script in conrtab of Master server

#chmod +x /path/to/Checkreplication.sh

#crontab -e

* * * * * /path/to/Checkreplication.sh

Database Replication In MySQL

Database Replication In MySQL

 

Configure the MySQL Master Server

Step 1 : edit /etc/mysql/my.cnf file. 

#skip-networking

#bind-address            = 127.0.0.1

(add below line in /etc/mysql/my.cnf file)

server-id               = 1

log_bin                 = /var/log/mysql/mysql-bin.log

binlog_do_db            = replicationdb

step 2 :  Restart Mysql server

#/etc/init.d/mysql restart

Step 3 : create a user with replication privileges:

#mysql -u root -p

mysql> GRANT REPLICATION SLAVE ON *.* TO ’replicationuser’@’%’ IDENTIFIED BY ‘<some_password>’;

mysql>FLUSH PRIVILEGES;

mysql>USE replicationdb;

mysql>FLUSH TABLES WITH READ LOCK;

mysql>SHOW MASTER STATUS;

Result of above command:

+—————+———-+————–+——————+

| File          | Position | Binlog_do_db | Binlog_ignore_db |

+—————+———-+————–+——————+

| mysql-bin.005 | 180      | replicationdb    |                  |

+—————+———-+————–+——————+

1 row in set (0.00 sec)

Please remeber above information will need in slave server configuration

mysql>quit;

Step 4 : Dump replicationdb from the Master server

#mysqldump -u root -p<password> replicationdb > replicationdb.sql

Above command create  dump of replicationdb in the file replication.sql. Transfer this file to your slave server!

Step  5 : Unlock the tables

#mysql -u root -p

mysql>UNLOCK TABLES;

mysql>quit;

 

Configure The Slave Server

Step 1 : Create the database replicationdb

#mysql -u root -p
mysql>CREATE DATABASE replicationdb;
mysql>quit;

Step 2 : Extract the dump database in replicationdb database

#mysql -u root -p<password> replicationdb < /path/to/replicationdb.sql

Step 3 : Add the below lines into /etc/mysql/my.cnf

server-id=2

master-host=192.168.10.175

master-user=replicationuser

master-password=secret

replicate-do-db=replicationdb

Step 3 : Restart MySQL:

# /etc/init.d/mysql restart

Step 4 :  Allow slave user to connet to Remote Master server
# mysql -u root -p

mysql>SLAVE STOP;

mysql>CHANGE MASTER TO MASTER_HOST=’192.168.10.175?, MASTER_USER=’replicationuser’, MASTER_PASSWORD=’<some_password>’, MASTER_LOG_FILE=’mysql-bin.005?, MASTER_LOG_POS=180;

mysql>START SLAVE;

mysql>quit;

Configuration is Complete now!  So whenever replicationdb is updated on the master server, all changes will be replicated to replicationdb on the slave server.

Automatic Users Logout After a Period of Inactivity

* For SH/BASH/TCSH

Below script will implement a 5 minute idle time-out for the default /bin/bash shell.

Step 1: Create file autologut.sh

# vi /etc/profile.d/autologout.sh

Add New

Append the following code:

TMOUT=300

readonly TMOUT

export TMOUT

Step 2: Save and close the file. Set permissions:

# chmod +x /etc/profile.d/autologout.sh

 

*For tcsh version follow the below process

Step 1: Create file autologut.sh

# vi /etc/profile.d/autologout.csh
Append the following code:

set -r autologout 5

Step 2: Save and close the file. Set permissions:

# chmod +x /etc/profile.d/autologout.csh

 

For SSH user inactive interval we can define that in configuration file of the ssh server

Step 1 : Configure /etc/ssh/sshd_config file

# vi /etc/ssh/sshd config

Find ClientAliveInterval and set to 300 (5 minutes) as follows:

ClientAliveInterval 300

ClientAliveCountMax 0

Step 2: Save and close the file. Restart sshd:
# service sshd restart

Duplicating a disk

If you have two IDE drives that are of identical size, and provided that you are sure they contain no bad sectors and provided neither are mounted, you can run

dd if=/dev/hda of=/dev/hdb

To copy the entire disk and avoid having to install an operating system from scratch. It doesn’t matter what is on the original (Windows, LINUX, or whatever) since each sector is identically duplicated; the new system will work perfectly.

How to Use MD5 Sum

Full Name : Message-Digest algorithm 5

Usage : Using an MD5 checksum you can  verify the integrity of data

Algorithm : cryptographic hash function with a 128-bit value

MD5 sum first identify the the data which is backup and then create a MD5 checksum which is combination of unique string of letters and numbers put together string like : 3dfsdjl2342ldkfjkdf32k. MD5 checksums are very useful for the verification of data and for passwords

 

Check MD5 sum usage

Step 1 : Generate a MD5 checksum:

Go to in Shell console

#md5sum filename > filename.md5?

Step 2: Verify a MD5 checksum:

#md5sum -c filename.md5?

Remove Blank lines from file

   sed ‘/./!d’ filename.txt > temp1.txt

 sed ‘/^$/d’ filename.txt > temp1.txt

  grep -v ‘^$’ filename.txt > temp1.txt

Disable Users to login into the server

Suppose we want to take a backup of user’s account. So for that first of all we have to block the user to login into their account to maintain data integrity of user’s backup files. So using below technique we can do that very easily.enjoy !!!

Edit the pam file located in /etc/pam.d/ directory for the service you want to control.

Example : Suppose you want to do control  ssh service

Step 1: Add below line in /etc/pam.d/sshd file if it is not available.

account required pam_nologin.so

Step 2: Create the /etc/nologin file,

# touch /etc/nologin

This should disable the login from ssh for every user except administrator user(root).

Step 3: To re-enable the login just remove /etc/nologin

# rm –rf /etc/nologin

Time bases iptables rules

f you want to restrict/allow access to certain service on timely basis using iptables.

Use : iptables patch-o-matic extension (pom or p-o-m)

That allows us to match a packet based on its arrival or departure  timestamp.

Syntax : iptables RULE -m time –timestart TIME –timestop TIME –days DAYS -j ACTION

–timestart TIME: Time start value (format is 00:00-23:59)

–timestop TIME: Time stop value (the same format)

–days DAYS: a list of days to apply, from (format: Mon, Tue, Wed, Thu, Fri, Sat, Sun).

Example :  We want to  restrict access to SSH between 10:00 pm – 8:00am on weekdays.

#iptables -A INPUT -p tcp -d 192.168.10.1 –dport 22 -m time –timestart 22:00 –timestop 8:00 -days Mon,Tue,Wed,Thu,Fri -j DROP

Split the Large files

Split on a 300mb example.zip file:

#split -b 100mb example.zip

It will generate 3 files with the following file sizes:

100MB xaa
100MB xab
100MB xac

After split use: cat to combine a file

#cat xa* > example-new.zip

Block brute force attacks using denyhost

ssue:

Check your /var/log/auth.log file. In this file you can able to see that some unauthorized user is constantly trying to access your system using brute force attacks. He/she trying to get the access of your system by some scripts which is trying to login with different password. We can block this using simple iptables rule.

Step 1 : Install Denyhost tools in your system.

apt-get install denyhost

Step 2 : Configure /etc/denyhost.conf file based on your requirement

* configure this file to anylize the log file

SECURE_LOG = /var/log/auth.log(default)

DENY_THRESHOLD_INVALID = 3 (will  block each host after the number of failed login)

DENY_THRESHOLD_VALID = 5  (will  block each host after the number of failed login for valid user login attempts)

 

DENY_THRESHOLD_ROOT = 3 (for root login)

Step 3 : Restat denyhost

#/etc/init.d/denyhost restart