How to update system time w/o ntpdate

Recently I tried to update time on the server and there was no way to use ntpdate because it was blocked and update raised the following error:

<p>ntpdate pool.ntp.org 6 Nov 21:18:08 ntpdate[23312]: no server suitable for synchronization found</p>

I used this trick to workaround the issue:

date; date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"; date

How to find unused IP in subnetwork

Working with many docker containers on one machine I needed to get list of free IP addresses in specific docker network. This is the way:


comm -3 <(nmap -sL 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}' | sort) <(nmap -sn 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}' | sort) | sort -V
172.30.0.9
172.30.0.10
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15

Let me explain.

This generates the list of IPs in subnetwork:

nmap -sL 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}'
172.30.0.8
172.30.0.9
172.30.0.10
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15

This generates list of used IPs in subnetwork:

nmap -sn 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}' 
172.30.0.8

The comm compares two lists and suppress common lines

comm - compare two sorted files line by line
       -3     suppress column 3 (lines that appear in both files)

One more example:

cat ip_pool.txt
172.30.0.8
172.30.0.9
172.30.0.10
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15

cat ip_used.txt
172.30.0.8
172.30.0.10

comm -3 <(cat ip_pool.txt | sort) <(cat ip_used.txt | sort) | sort -V
172.30.0.9
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15


Restart slave thread in MySQL when specific error occurred

On one of slaves I got error:

write failed: No space left on device (28)

I also found that slave got 1062 error (“Duplicate entry”) and stopped. I cleaned up some free space (old logs). When I tried to restart it with pt-slave-restart then I found that IO_Thread downloads binlogs from master and uses all free space again.

As a workaround – I decide to start just SQL_Thread, let it process all relay logs and then start IO_Thread again.

This is quick bash oneliner I created, which checks replication and if 1062 error exists then does skipping and starting SQL_Thread again.

while true; do if [[ $(mysql -e "show slave status\G" | grep "Last_SQL_Errno: 1062" -c) -gt 0 ]]; then mysql -e "set global sql_slave_skip_counter=1; start slave sql_thread;"; fi; done

This was enough to get issue fixed.

How to clear all iptables rules and allow everything

How to clear all iptables rules and allow everything

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

Increase docker’s containers size

By default containers are limited to size 10G. I needed to increase container size. For this I added option to file /etc/docker/daemon.json:

{"storage-driver": "overlay"}

After this I restarted docker. Each container now gets total amount of space available on the mount point.

Remove line from history with grep

Today I needed to clear history from some command. I looked into history and used grep to filtering command I needed.

for i in $(history | grep something | grep -oE "[0-9]{1,} " | sort -rn); do history -d ${i}; done

How to check if UDP port is opened on remote host

nmap -sU -p PORT IP

For example Galera requires 4567/udp port opened and I needed to check if it’s opened on one of the nodes.

root@host:~# nmap -sU -p 4567 10.10.20.20

Starting Nmap 7.01 ( https://nmap.org ) at 2018-02-04 10:04 UTC
Nmap scan report for 10.10.20.20
Host is up (0.00043s latency).
PORT     STATE  SERVICE
4567/udp closed unknown
MAC Address: AB:AB:AB:AB:AB:AB (Unknown)

Nmap done: 1 IP address (1 host up) scanned in 0.52 seconds

Get dump of all routines of MySQL server

Dump of all routines of MySQL:

mysqldump --routines --no-create-info --no-data --no-create-db --skip-opt --all-databases &gt; `hostname -s`_stored_procedures.sql

Graceful view of MySQL GRANTS

Just found this script. It allows to output grants in graceful way:

mysql -B -N $@ -e "SELECT DISTINCT CONCAT(
'SHOW GRANTS FOR \'', user, '\'@\'', host, '\';'
) AS query FROM mysql.user" | \
mysql $@ | \
sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}'

Continue reading