Showing posts with label Daily Sysadmin. Show all posts
Showing posts with label Daily Sysadmin. Show all posts

Tuesday, May 26, 2015

Ubuntu 14.04 - Mysql Galera Cluster for Wordpress

This cluster has 2 nodes run in multi master mode

Installing cluster
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://mirror3.layerjet.com/mariadb/repo/5.5/ubuntu trusty main'
sudo apt-get update -y ; sudo apt-get install -y galera mariadb-galera-server rsync

Configuring cluster
On each of the host in the cluster add this configuration
vi /etc/mysql/conf.d/galera.cnf
[mysqld]
#mysql settings
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_size=0
query_cache_type=0
bind-address=0.0.0.0
#galera settings
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_name="my_wsrep_cluster"
wsrep_cluster_address="gcomm://192.20.3.31,192.20.3.32"
wsrep_sst_method=rsync

Stop MariaDB instance in all of the Galera hosts
sudo service mysql stop

Init the Galera cluster on my 1st node by start MariaDB
sudo service mysql start --wsrep-new-cluster

Just start MariaDB on my 2nd node
sudo service mysql start

To prevent startup error on 2nd node we need to copy /etc/mysql/debian.cnf content from node1 to node2 and then restart MariaDB on 2nd node

Confirm cluster status has started
mysql -u root -p -e 'SELECT VARIABLE_VALUE as "cluster size" FROM INFORMATION_SCHEMA.GLOBAL_STATUS WHERE VARIABLE_NAME="wsrep_cluster_size"'
Enter password: 
+--------------+
| cluster size |
+--------------+
| 2            |
+--------------+
From now on the data will sync between both hosts. To test it try create one database from 1st node then go to 2nd node and see it is there.
On 1st node
mysql -u root -p
create database HelloWorld;

On 2nd node
mysql -u root -p
show databases;
if Helloworld database was there. That's kool. We're ready to go.

Grant Remote privilege to remote user on DBcluster
On one of the DB host (node1 or node2)
mysql -u root -p
create user 'handsome'@'%' identified by handsome_password;
grant all privileges on Database_name . * to  'handsome'@'%';
flush privileges;
show grants for 'handsome'@'%';

Note: 
Replace handsome with your username or root
Replace Database_name with your DB name or * to grant privileges on all Database

DNS setting
On my premise DNS server I add a Round Robin DNS record point to 2 Galera host
dbcluster -> 192.20.3.31
dbcluster -> 192.20.3.32

On my PHP app server I can connect to the dbcluster with this command
mysql -u root -p -h dbcluster 

If it not work, you could have a look on MariaDB log

Reference link:
https://www.linode.com/docs/databases/mariadb/clustering-with-mariadb-and-galera

Monday, December 15, 2014

Ubuntu server 14.04 - Auto mount network Drive Issue

Recently, due to maintanance upgrade I have to reboot my Server and notice that it stuck at boot screen waiting for external Network drive to mount. Previous article ubuntu-server-auto-mount-iscsi-storage

The message is "the disk drive for /backupdata is not ready or not presented". 2 things I can do:

Remount network drive with UUID instead of /dev/blah..blah in /etc/fstab
#> blkid
/dev/mapper/datavg-backupdata: UUID="85e81bbc-2c86-4bef-b1e9-f77d21929de5" TYPE="xfs" 

Add nobootwait option into mount target in /etc/fstab
UUID=85e81bbc-2c86-4bef-b1e9-f77d21929de5 /backupdata xfs auto,rw,user,nobootwait 0 0

Then reboot my system and It work!

Thursday, December 11, 2014

Backuppc add administrator account

Adding a CGI Administrator

as root privilege open up /etc/backuppc/config.pl and go to line 2020
$Conf{CgiAdminUserGroup} = 'backuppc';
$Conf{CgiAdminUsers} = 'backuppc, admin';

Reload Backuppc service
service backuppc reload

Adding Administrator to htpasswd for Apache basic authentication

htpasswd /etc/backuppc/htpasswd admin

Restart Apache service
service apache2 restart

http://wiki.zmanda.com/index.php/BackupPC_Web_Interface

Tuesday, December 9, 2014

Ubuntu server auto mount iSCSi storage

Install Open-iSCSI initiator
apt-get install open-iscsi

configuration file could be located at /etc/iscsi/iscsid.conf or ~/.iscsid.conf.
node.startup = automatic
node.session.auth.username = MY-ISCSI-USER
node.session.auth.password = MY-ISCSI-PASSWORD
discovery.sendtargets.auth.username = MY-ISCSI-USER
discovery.sendtargets.auth.password = MY-ISCSI-PASSWORD

Discover iSCSi Target
iscsiadm -m discovery -t sendtargets -p 172.31.1.150

Login to target
iscsiadm --mode node --targetname iqn.1992-04.com.emc:cx.fcnma082600013.a0 --portal 172.31.1.150 --login

Note: remember login to your iSCSi server (SAN) then allow your server connection
Make sure you auto connect to iSCSi server target when server start up
vi /etc/iscsi/nodes/iqn.1992-04.com.emc\:cx.fcnma082600013.b1/172.31.2.151\,3260\,3/default

node.startup = automatic

Restart service
/etc/init.d/open-iscsi restart

Format and mount the iSCSi volume

dmesg or fdisk -l, or look at /var/log/messages to find new devices
You can use LVM or partition your disk now
fdisk /dev/sde

Format a partition ext4 or xfs or brtfs
mkfs.ext4 /dev/sde1

Mount file system
mkdir /backupdata
mount /dev/sde1 /backupdata

Add mount point to /etc/fstab 
/dev/sde1  /backupdata ext4 defaults 0 0

Monday, December 8, 2014

Bonding Network Card Ubuntu 14.04

Make sure your Cisco Switch support 802.3ad, then group 2 interface ports together
Router> enable
Router# configure terminal
Router(config)# interface port-channel 1
Router(config-if)# interface g1/0/24
Router(config-if)# channel-group 1 mode active
Router(config-if)# exit
Router(config)# interface g1/0/23
Router(config-if)# channel-group 1 mode active
Router(config-if)# end
Router# copy run start

Bonding mode: http://www.mjmwired.net/kernel/Documentation/networking/bonding.txt#508
802.3ad or 4 : IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.

Bonding network card on Ubuntu Server
apt-get install ifenslave-2.6
echo "bonding" >> /etc/modules
modprobe bonding

vi /etc/network/interface
auto em1
iface em1 inet manual
bond-master bond0
bond-primary em1

auto em2
iface em2 inet manual
bond-master bond0

auto bond0
iface bond0 inet static
bond-slaves none
bond-mode 4
bond-miimon 100
        address 192.168.1.111
        netmask 255.255.0.0
        network 192.168.0.0
        broadcast 192.168.255.255
        gateway 192.168.1.1
        dns-nameservers 192.168.1.1 

Reboot server and wait a little bit for the bond interface become active

http://www.paulmellors.net/ubuntu-server-14-04-lts-nic-bonding/
http://ilostmynotes.blogspot.com/2009/04/bridging-bonded-network-interface-on.html

Thursday, December 4, 2014

Ubuntu - 3 tricks to copy DVD into iso file

Built in command

cat /dev/sr0 > /home/Documents/mydisk.iso
dd if=/dev/sr0 of=/home/Documents/mydisk.iso

If both cat, and dd not work because of '/dev/sr0': Input/output error
If I run dmesg, I recieve a lot of Buffer I/O error on device sr0

Try ddrescue in Ubuntu the package is gddrescue
sudo apt-get install gddrescue
ddrescue --direct --block-size=2048 --no-split /dev/sr0 /home/Documents/mydisk.iso

Now you can open iso file with VLC

Tuesday, December 2, 2014

Linux find command for files or folders older than .....

Find files and folders in current working directory older than 60 days
find ./* -maxdepth 0 -mtime +60

Find only folders in /mnt/media -> child folder -> grandchild exclude these folder notme, dtran, xXXx older than 3 years
find /mnt/media -maxdepth 2 -mtime +1095 -type d -not \( -path "./notme*" -o -path "./dtran*" -o -path "./xXXx*" \) > ~/Folder2Backup.txt

Remove all files that have xxx in names and older than 2 years
find ./ -type f -name "*xxx*.*" -mtime +730 -exec rm "{}" \;

Windows not a big deal, install Cygwin and Finding Nemoooo
find /cygdrive/f -maxdepth 2 -type d -mtime +730 > ~/dir_older_than_2years.txt

Find directory older than 2 years and list its size
find /cygdrive/f -maxdepth 1 -type d -mtime +730 -exec du -sh "{}" \; 

Apple flush DNS cache

Open Terminal and run this command

dscacheutil -flushcache

TTL and DNS cache

Time to Live (TTL) is used to renew the DNS records on all the DNS servers around the world to the set TTL value. 

The units used are seconds. The older common TTL value for DNS was 86400 seconds, which is 24 hours. A TTL value of 86400 would mean that, if a DNS record was changed on the authoritative name server, the DNS servers around the world could still be showing the old value from their cache for up to 24 hours after the change.

Why change the TTL value

If you want to make quick DNS changes, it is a good practice to place a low TTL value. The TTL values can be changed by logging in to the the Self Service Center.
If you change the TTL, you have to wait for the current TTL to pass before the new TTL will be active. This means that if you change the TTL to 1 hour instead of 24 hours, you have to wait the 24 hours first before the 1 hour TTL wil be active.

Setting a TTL value lower than 12 hours is not recommended, and should only be used during a short period for special cases. By default, the TTL value is 24 hours.

Friday, November 28, 2014

Listing directory in Graphical format in Bash

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

Script from centerkey.com: http://www.centerkey.com/tree/tree.sh
#!/bin/sh
#######################################################
#  UNIX TREE                                          #
#  Version: 2.3                                       #
#  File: ~/apps/tree/tree.sh                          #
#                                                     #
#  Displays Structure of Directory Hierarchy          #
#  -------------------------------------------------  #
#  This tiny script uses "ls", "grep", and "sed"      #
#  in a single command to show the nesting of         #
#  sub-directories.  The setup command for PATH       #
#  works with the Bash shell (the Mac OS X default).  #
#                                                     #
#  Setup:                                             #
#     $ cd ~/apps/tree                                #
#     $ chmod u+x tree.sh                             #
#     $ ln -s ~/apps/tree/tree.sh ~/bin/tree          #
#     $ echo "PATH=~/bin:\${PATH}" >> ~/.profile      #
#                                                     #
#  Usage:                                             #
#     $ tree [directory]                              #
#                                                     #
#  Examples:                                          #
#     $ tree                                          #
#     $ tree /etc/opt                                 #
#     $ tree ..                                       #
#                                                     #
#  Public Domain Software -- Free to Use as You Like  #
#  http://www.centerkey.com/tree  -  By Dem Pilafian  #
#######################################################
echo
if [ "$1" != "" ]  #if parameter exists, use as base folder
   then cd "$1"
   fi
pwd
ls -R | grep ":$" |   \
   sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
# 1st sed: remove colons
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph three spaces
# 4th sed: replace first dash with a vertical bar
if [ `ls -F -1 | grep "/" | wc -l` = 0 ]   # check if no folders
   then echo "   -> no sub-directories"
   fi
echo
exit

Add it to your system. Please watch out the code before running it!
curl http://www.centerkey.com/tree/tree.sh > /usr/bin/tree
chmod +x /usr/bin/tree
 
 http://stackoverflow.com/questions/3455625/linux-command-to-print-directory-structure-in-the-form-of-a-tree

Compare 2 directory in Linux

Let's make an example. Create 2 folder and put files in it
mkdir dir{1,2}
touch dir1/{1,2,3,4,5,6}.txt
touch dir2/{2,4,5,7,9}.txt

ls -R dir1
dir1:
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt

ls -R dir2
dir2:
2.txt  4.txt  5.txt  7.txt  9.txt

diff -r dir1 dir2
Only in dir1: 1.txt
Only in dir1: 3.txt
Only in dir1: 6.txt
Only in dir2: 7.txt
Only in dir2: 9.txt

http://stackoverflow.com/questions/16787916/difference-between-2-directories-in-linux

Ubuntu Change Date from IST to ICT

If you try to change your clock, NTP setting and sync to hardware clock but the date still wrong so here's how
#date
Thu Nov 27 08:16:23 IST 2014  #wrong date time
#dpkg-reconfigure tzdata
Current default time zone: 'Asia/Ho_Chi_Minh'
Local time is now: Thu Nov 27 09:48:02 ICT 2014.
Universal Time is now: Thu Nov 27 02:48:02 UTC 2014.

#date
Thu Nov 27 09:48:09 ICT 2014  #correct now