Monday, June 15, 2020

How to dump the database with triggers and procedures?




Stored procedures and Triggers are first introduced with MySQL 5.0. So if you are still using MySQL older version’s upgrade it to MySQL 5.0 or higher version to use these features.




What is Stored Procedure ?


A stored procedure, by definition, is a segment of declarative SQL code which is stored in the database catalog and can be invoked later by a program, a trigger or even a stored procedure.


What is Triggers ?


Triggers are event-driven specialized procedures, they are stored in and managed by the database. A trigger is a SQL procedure that initiates an action on an event (Like INSERT, DELETE or UPDATE) occurs.



mysqldump will backup by default all the triggers but NOT the stored procedures/functions. There are 2 mysqldump parameters that control this behavior:


--routines - FALSE by default

--triggers - TRUE by default


This means that if you want to include in an existing backup script also the triggers and stored procedures you only need to add the --routines command line parameter:


Backup Stored Procedures and Routines


We need to specify --routines to take backup of stored procedures with data and tables.


The following command will take backup of entire database including stored procedures. For example, your database name is “mydb”.


mysqldump -u root -p --routines mydb > mydb.sql


To take backup of only Stored Procedures and Triggers (Exclude table and data ) use the following command.


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



To Take backup without triggers use following command

mysqldump -uroot -p --skip-triggers mydb> mydb.sql

Restore Procedures


To restore stored procedures in the database simply use the following command, But make sure you have taken backup properly before restoring it to avoid any data loss.


mysql -u root -p mydb < mydb.sql


Tuesday, April 28, 2020

how to find my public ip address in linux ?

Using dig


dig (domain information groper) is a simple command line utility for probing DNS name servers. To find your public IP addresses, use the opendns.com resolver as in the command below:


Syntax:


dig +short myip.opendns.com @resolver1.opendns.com


Example:


dig +short myip.opendns.com @resolver1.opendns.com


111.125.200.200


Using host


host command is an easy-to-use command line utility for carrying out DNS lookups. The command below will help to display your system public IP address.


Syntax:
host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'


Example:


host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'
111.125.200.200


Using wget


curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following
commands display your public IP address.


Syntax:


wget -qO- http://ipecho.net/plain | xargs echo


Or


wget -qO - icanhazip.com


Example:


 wget -qO- http://ipecho.net/plain | xargs echo
111.125.200.200


 wget -qO- http://ipecho.net/plain | xargs echo
111.125.200.200


Using curl


curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following
commands display your public IP address.


Syntax:


curl ifconfig.me


Example:
curl ifconfig.me
111.125.200.200


The following commands will get you the IP address list to find public IP addresses for your machine:

  • curl ifconfig.me
  • curl icanhazip.com
  • curl ipinfo.io/ip
  • curl api.ipify.org
  • curl checkip.dyndns.org
  • curl ident.me
  • curl bot.whatismyipaddress.com
  • curl ipecho.net/plain

Monday, April 20, 2020

nc command examples in linux?

Netcat or nc is a networking utility for debugging and investigating the network.

This utility can be used for creating TCP/UDP connections and investigating them.
The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.



How to Install and Use Netcat in Linux
To install the netcat package on your system, use the default package manager for your
Linux distribution.


[On CentOS/RHEL]

$ yum install nc                  

 [On Fedora 22+ and RHEL 8]
$ dnf install nc                 

 [On Debian/Ubuntu]
$ sudo apt-get install Netcat    


Port Scanning

Netcat can be used for port scanning: to know which ports are open and running services on
a target machine. It can scan a single or multiple or a range of open ports.

 It may be useful to know which ports are open and running services on a target machine.
  The -z flag can be used to tell nc to report open ports, rather than initiate a connection.
Usually it's useful to turn on verbose output to stderr by use this option in conjunction with -
v    option.


Where 

    -v      Have nc give more verbose output.
    -w    timeout      Connections which cannot be established or are idle timeout after timeout
seconds.   The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag.  The default is no timeout.

    -z      Specifies that nc should just scan for listening daemons, without sending any data
to them.  It is an error to use this option in con‐junction with the -l option.

  -n      Do not do any DNS or service lookups on any specified addresses, hostnames or
ports.





  #scan a single port
$ nc -v -w 2 z 192.168.56.156 22   

Example:
    
 nc -v -w 2 -z 192.168.56.156 22 
 Connection to 192.168.56.156 22 port [tcp/ssh] succeeded!


#scan multiple ports

$ nc -v -w 2 z 192.168.56.156 22 80  

 #scan range of ports

$ nc -v -w 2 z 192.168.56.156 20-22 
Example:

nc -v -w 2 -z 192.168.56.156 20-22
nc: connect to 192.168.56.156 port 20 (tcp) failed: Connection refused
nc: connect to 192.168.56.156 port 21 (tcp) failed: Connection refused
Connection to 192.168.56.156 22 port [tcp/ssh] succeeded!





Find a Service Running on Port

You can also use Netcat to obtain port banners. In this case, it will tell you what service is
running behind a certain port. For example to know what type of service is running behind
port 22 on a specific server, run the following command

nc -v -n 192.168.56.156 22
Connection to 192.168.56.156 22 port [tcp/*] succeeded!
SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7

Wednesday, January 15, 2020

Which directory is that bash script in?

There may be times when you need to know the actual location a BASH script is located
within the script. This can be done with a combination of the $0 value and the dirname
command.

The $0 value

A BASH script makes the first command line argument available as $1, the second as $2
and so on. The command run, exactly as it was called but without the command line
arguments, is stored in $0

The dirname command

The dirname command returns the directory name part of a filename. Note that the directory
and/or file do not actually exists; dirname simply strips the last component of a file path.

For example, "dirname /a/b/c" will echo "/a/b", "dirname ../a/b/c" will echo "../a/b", "dirname
../" will echo "." and so on.

Putting it all together
The directory the BASH script is located can be retrieved using dirname $0 like so:

DIRECTORY=`dirname $0`

BUT note that this may be a relative path and not necessarily an absolute one, depending 
how the script is called. Take the following script, for example, which saves the directory
name to a variable for later use, and then echos it:

#!/bin/bash
DIRECTORY=`dirname $0`
echo $DIRECTORY
If the file is saved at /home/arun/bin/test1.sh, the permissions are changed so it can be
executed (chmod 0700 ~/bin/test1.sh) and it is run from /home/arun like so:

/home/arun/bin/test1.sh
then it will echo this:

/home/arun/bin
It it is run like so:

bin/test1.sh
then it will echo this:

bin
Because the path used to run the script in the second example is a relative path, "dirname
$0" only echo’s "bin", which may not be suitable.

Always return the absolute path

To always return the absolute path to the directory the script is located, change to the scripts
directory and get the dirname like so, again saving the value to a variable for later use and
then echoing it:

#!/bin/bash
DIRECTORY=$(cd `dirname $0` && pwd)
echo $DIRECTORY

If this script is saved as /home/arun/bin/test2.sh , then whether it is run as
/home/arun/bin/test2.sh or bin/test2.sh or by some other path, it will always echo:

/home/arun/bin

Note that although the change directory command (cd) is used, the script will not change
directory and any other calls within it are still relative to the current working directory.

If we were running the above script in /home/arun, then calling "pwd" in the next line would
still echo "/home/arun"

Thursday, November 14, 2019

IPC Command Examples in linux ?

IPC Inter-Process-Communication

Shared memory is the memory that may be accessed by multiple processes; i.e. a memory
region that can be shared between different processes and a better way of passing data
between two processes. Shared memory is the fastest form of Inter-Process
Communication which is currently available. Assume that the program will create a memory
portion, which another process can access (if permitted). A shared segment can be
attached multiple times by the same process. Whenever the memory is mapped into the
address space of the process, i.e. sharing the common memory region, the kernel will not
involve while passing data between the processes.


Different Types of IPCS

There are various IPC’s which allows a process to communicate with another processes,
either in the same computer or different computer in the same network.

Pipes – Provides a way for processes to communicate with each another by exchanging messages. Named pipes provide a way for processes running on different computer systems to communicate over the network.

Shared Memory – Processes can exchange values in the shared memory. One process will create a portion of memory which other process can access.

Message Queue – It is a structured and ordered list of memory segments where processes store or retrieve data.

Semaphores – Provides a synchronizing mechanism for processes that are accessing the same resource. No data is passed with a semaphore; it simply coordinates access to shared
resources.

List all the IPC facility

ipcs command with -a option lists all the IPC facilities which has read access for the current
process. It provides details about message queue, semaphore and shared memory.
ipcs -a


>ipcs -a

------ Shared Memory Segments --------
key        shmid owner      perms bytes nattch     status      
0x00000000 1671168    ravi 600 524288     2 dest         
0x00000000 393217     ravi 600 524288     2 dest         
0x00000000 1277954    ravi 600 524288     2 dest         
0x00000000 1310723    ravi 600 134217728  2 dest         
0x00000000 622596     ravi 600 524288     2 dest         
0x00000000 884741     ravi 600 524288     2 dest         
0x00000000 983046     ravi 600 393216     2 dest         
0x00000000 1081351    ravi 600 524288     2 dest         
0x00000000 2818056    ravi 600 524288     2 dest         

------ Semaphore Arrays --------
key        semid owner      perms nsems     

------ Message Queues --------
key        msqid owner      perms used-bytes   messages    


All the IPC facility has unique key and identifier, which is used to identify an IPC facility.

List all the Message Queue

ipcs with option -q, lists only message queues for which the current process has read
access.

ipcs -q

------ Message Queues --------
key        msqid owner      perms used-bytes   messages
0x000005a4 32768      root 644 0            0

List all the Semaphores

ipcs -s option is used to list the accessible semaphores.

ipcs -s

------ Semaphore Arrays --------
key        semid owner      perms nsems
0x0103eefd 0          root 664 1
0x0103eefe 32769      root 664 1

List all the Shared Memory

ipcs -m option with ipcs command lists the shared memories.

ipcs -m

------ Shared Memory Segments --------
key        shmid owner      perms bytes nattch     status      
0x00000000 1671168    ravi 600 524288     2 dest         
0x00000000 393217     ravi 600 524288     2 dest         
0x00000000 1277954    ravi 600 524288     2 dest         
0x00000000 1310723    ravi 600 134217728  2 dest     

Lists the Limits for IPC facility

ipcs -l option gives the system limits for each ipc facility.

ipcs -l

------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 18014398509465599
max total shared memory (kbytes) = 18446744073642442748
min seg size (bytes) = 1

------ Semaphore Limits --------
max number of arrays = 32000
max semaphores per array = 32000
max semaphores system wide = 1024000000
max ops per semop call = 500
semaphore max value = 32767

------ Messages Limits --------
max queues system wide = 32000
max size of message (bytes) = 8192
default max size of queue (bytes) = 16384

List Creator and Owner Details for IPC Facility

ipcs -c option lists creator userid and groupid and owner userid and group id. This option
can be combined with -m, -s and -q to view the creator details for specific IPC facility.

ipcs -m -c

------ Shared Memory Segment Creators/Owners --------
shmid      perms cuid       cgid uid gid       
1671168    600 ravi        ravi ravi ravi       
393217     600 ravi        ravi ravi ravi       
1277954    600 ravi        ravi ravi ravi       
1310723    600 ravi        ravi ravi ravi       

Process ids that accessed IPC facility recently

ipcs -p option displays creator id, and process id which accessed the corresponding ipc
facility very recently.

ipcs -m -p

------ Shared Memory Creator/Last-op PIDs --------
shmid      owner cpid       lpid      
1671168    ravi 3124       10093     
393217     ravi 2647       2981      
1277954    ravi 2836       24419     
1310723    ravi 2836       24419     


Last Accessed Time

ipcs -t option displays last operation time in each ipc facility. This option can also be
combined with -m, -s or -q to print for specific type of ipc facility. For message queue, -t
option displays last sent and receive time, for shared memory it displays last attached
(portion of memory) and detached timestamp and for semaphore it displays last operation
and changed time details.

ipcs  -t

------ Shared Memory Attach/Detach/Change Times --------
shmid      owner attached             detached changed             
1671168    ravi Nov 14 08:12:16      Nov 14 08:12:16 Nov 6 19:20:58     
393217     ravi Nov  6 19:20:15 Nov  6 19:20:15 Nov 6 19:19:50     


 Status of current usage

ipcs with -u command displays current usage for all the IPC facility. This option can be
combined with a specific option to display the status for a particular IPC facility.

ipcs -u

------ Shared Memory Status --------
segments allocated 24
pages allocated 53664
pages resident  10535
pages swapped   691
Swap performance: 0 attempts 0 successes

------ Semaphore Status --------
used arrays = 49
allocated semaphores = 252

------ Messages: Status --------
allocated queues = 1
used headers = 0
used space = 0 bytes


How to Delete/remove a share memory ?

Delete/remove a share memory
ipcrm
usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]
          [-Q msgkey] [-M shmkey] [-S semkey] ... ]

Delete/remove a Shared Memory segment
$ ipcrm -m 10715155

Delete a message queue
$ipcrm -s 163843