Monday, August 15, 2016

lvm snap shot for mysql backup

Logical Volume Manager (LVM)

The Logical Volume Manager (LVM) is a block device subsystem provided by Linux that sits between the filesystem (ext3/4, xfs, etc.) and the physical disks (/dev/sda, /dev/sdb, etc.). It acts as a translation layer that allows some very advanced operations, such as the snapshot feature covered here, and is completely transparent to the filesystem.
Configuring LVM is outside the scope of this article, but most mainstream Linux distributions provide it as an option during installation, and many even use it by default. One thing to note, though, is that it is important to reserve some of the physical drive space as unallocated. The unallocated space is what allows snapshots to be created. Generally speaking, a good practice is to allocate the amount of space currently required plus 10% for growth. Unlike traditional disk partitions, expanding an LVM volume is quick, easy, and does not require any downtime, so there is no danger to underestimating at this stage.
Now, for the bad news. Many cloud providers do not support LVM. But, for those servers that can use LVM, the snapshot feature can be used to get a point-in-time backup of MySQL with minimal table locking.

Discovery Phase

Before proceeding, some preliminary information is needed.
  1. Where are the MySQL data files stored?
    ~# mysqladmin variables | grep datadir | datadir | /var/lib/mysql/ |
    This shows that the data files are located at /var/lib/mysql.
  2. Which logical volume hosts this location?
    ~# df /var/lib/mysql Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg0-var 209698268 45532856 164165412 22% /var
    This shows that the volume group is vg0, and the logical volume name is var. So, the full block device path that the LVM tools will understand is /dev/vg0/var. This can be confirmed with the following
    ~# lvscan ACTIVE '/dev/vg0/var' [200.00 GB] inherit
  3. How much unallocated space is available for the snapshot in the vg0 volume group?
    ~# vgdisplay vg0 | grep Free Free PE / Size 9551 / 37.31 GB
    This shows that there are 37.31GB of free space on the vg0 volume group. This is important to note, because this free space is where changes to the live database are tracked while the snapshot is present.

Create an LVM Snapshot

  1. Connect to MySQL, flush the tables to disk, and lock them. Do not do this with mysqladmin, and be sure to leave the database session open. As soon as a client (such as mysqladmin) disconnects, this lock is lifted. In order to guarantee data integrity, the database must remained locked until the LVM snapshot is created. The amount of time that this operation takes will vary based on how much data needs to be flushed to disk, but it is generally very quick. FLUSH TABLES WITH READ LOCK
  2. In another terminal session, create the LVM snapshot. This snapshot needs to be large enough to accommodate the changes that will be made to the database while the snapshot is present. Because this snapshot will be short lived, the shortcut "100%FREE" can be used, which will use all 37.31GB of unallocated space in this case. This process is nearly instantaneous because LVM uses a copy-on-write (COW) snapshot method. lvcreate -l100%FREE -s -n mysql-backup /dev/vg0/var
  3. Back at the original MySQL session, release the read lock so that normal database operation can resume.
    UNLOCK TABLES
At this point, there is a consistent, point-in-time snapshot of the MySQL file structure stored in the LVM snapshot. The database can now go on with its business, and the only locking required was to flush any data from memory to disk.

Snapshot Magic

Using lvscan again, the new snapshot can be seen.
~# lvscan ACTIVE Original '/dev/vg0/var' [200.00 GB] inherit ACTIVE Snapshot '/dev/vg0/mysql-backup' [37.31 GB] inherit
The snapshot can now be mounted at an arbitrary location (If /dev/vg0/var is an XFS volume, add "-o nouuid -t xfs" to the mount command). mkdir -p /mnt/snapshot mount /dev/vg0/mysql-backup /mnt/snapshot
From here, any standard filesystem backup method can be used to store a copy of /var/lib/mysql as mounted under /mnt/snapshot. This method could range from rsync/ssh, to a simple tarball, to some enterprise backup solution.

Portability

In order to safely restore a filesystem level MySQL backup, it would need to be restored to a MySQL server with the same major and minor versions (5.0, vs 5.1, etc). While this is may not be a problem when using stock packages from a distribution, a traditional mysqldump backup is much more portable. However, as powerful as mysqldump is, it cannot create a dump file from a set of raw MySQL files, it must pull the data from an active MySQL server.
The solution is to start a second instance of MySQL using the data files from the snapshot mounted at /mnt/snapshot. There are three things to keep in mind for this second instance
  1. The TCP port must be different than the primary MySQL instance, which can be found with
    ~# mysqladmin variables | grep port | innodb_support_xa | ON | | large_files_support | ON | | port | 3306 |
  2. The MySQL socket must be different than the primary MySQL instance, which can be found with  
    | socket                          | /var/run/mysqld/mysqld.sock |
  3. ~# mysqladmin variables | grep socket
  4. The --innodb-log-file-size must be identical to the primary MySQL instance, which can be found with
    ~# mysqladmin variables | grep innodb_log_file_size | innodb_log_file_size | 268435456
Taking these values into consideration, start the second MySQL instance
mysqld_safe --no-defaults --port=3307 --socket=/var/run/mysqld/mysqld-snapshot.sock --datadir=/mnt/snapshot/lib/mysql --innodb-log-file-size=268435456 &
Now, a full mysqldump can be performed against this second MySQL instance, which can lock all it wants without affecting the primary instance. In order to ensure that mysqldump is using the second instance, specify the MySQL socket file.
mysqldump -S /var/run/mysqld/mysql-snapshot.sock | gzip > /path/to/mysql/backup.sql.gz

Cleanup

That's it! There is now a portable, consistent mysqldump file at /path/to/mysql/backup.sql.gz, and the only locking required was to flush the data to disk. All that is left is to clean up

1) Stop the second MySQL instance with mysqladmin

mysqladmin -S /var/run/mysqld/mysqld-snapshot.sock shutdown

2) Unmount the snapshot volume

umount /mnt/snapshot

3) Delete the LVM snapshot

lvremove /dev/vg0/mysql-backup


Saturday, August 13, 2016

nginx reverse proxy parse

 # HTTP - redirect all requests to HTTPS  
 server {  
   listen 80;  
   listen [::]:80 default_server ipv6only=on;  
   return 301 https://$host$request_uri;  
 }  
 # HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/  
 # through to Parse Server  
 server {  
     listen 443;  
     server_name www.example.com;  
     root /usr/share/nginx/html;  
     index index.html index.htm;  
     ssl on;  
     # Use certificate and key provided by Let's Encrypt:  
     ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;  
     ssl_certificate_key /etc/letsencrypt/live/www.topsoccerfan.com/privkey.pem;  
     ssl_session_timeout 5m;  
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
     ssl_prefer_server_ciphers on;  
     ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';  
     # Pass requests for /parse/ to Parse Server instance at localhost:1337  
     location / {  
         proxy_set_header X-Real-IP $remote_addr;  
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
         proxy_set_header X-NginX-Proxy true;  
         proxy_pass http://localhost:1337/;  
         proxy_ssl_session_reuse off;  
         proxy_set_header Host $http_host;  
         proxy_redirect off;  
     }  
   #   location / {  
   #       try_files $uri $uri/ =404;  
   #   }  
 }  

Wednesday, October 14, 2015

php thread save vs unsafe

Thread safe is a good thing, it means whilst there may be multiple concurrent threads, they are talking to each other in a safe way that won't have race conditions, concurrency issues, etc.
Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it only manipulates shared data structures in a thread-safe manner, which enables safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures.

Wednesday, July 15, 2015

basic php

php

$third = $first;
$third .=$second;
echo $third;  #appending the string to variable

echo strtolower($third);
echo strtoupper($third);
echo ucfirst($third);  #firs letter of the string is capital
echo ucwords($third);    #first letter of words are capital case

##looping
##

<?php
$count=0;


?>
#########url encoding   it is legacy
get request is saved in $_GET =associative array
urlencode is for get request only

alphanumeric,underscore and dash is unchanged
reserved chars become %+2digit hexadecimal
reserved chars are !@#$%^&*()?.,; etc  not <,>
space become +

#####rawurlencode
similar to urlencode
space become %20

Rawurlencode is more compatible generally

urlencode: use it to encode the query string
-Query string is part after the "?"
=Space are better encoded as "+"



rawurlencode:
use it on path ,portion comes before the "?"
space must be encoded as %20
(help ful for apache and filesystem to find the page filesystem)


htmlspecialchars($string1)

include() = Dont throw error if files is not found[continued script]
require() = Throws error if file is not found[script stopped]
include_once()

###cookies
cookies can store up to 4000 characters maximum to store
cookies are stored in client browser

#pros of session:-
more storage
smaller request sizes(only sessionid is sent instead of eg 4000 char cookie)
conceal datavalues, more secure and less hackable

#cons of session
-slower to access
-expire when browser is closed
-session files accumulate


mysql_fetch_row
-result are in standard array-key are integers

mysqli_fetch_assoc
-result are in associative array
-key are column names

mysqli_fetch_array
-result are in array or associative array
-mysql_num,mysql_assoc,mysql_both

Recommended is mysqli_fetch_assoc [easier]





$name1="test2";
$value1=25;
$expire1 = time() + (60*60*24*7); #1week
setcookie($name1,$value1,$expire1);
//setcookie ($nameofcookie,$valueofcookie,$expirydateofcookie)


$test = isset($_COOKIE["test"]) ? $_COOKIE["test"] : "";
## unset cookies

setcookie($name, null, time()-3600)

###########

mysqli_fetch_row
=results are in a standard array
=keys are integers

mysqli_fetch_assoc
=result are in an associative array
=keys are column names

mysqli_fetch_array
=result are in either or both types of arrays
=MYSQL_NUM,MYSQL_ASSOC,MYSQL_BOTH


Monday, September 29, 2014

laravel setup in ubuntu 14.04.1

PHP >= 5.4
MCrypt PHP Extension

#apt-get install php5-mcrypt

#php5enmod mcrypt

urgen@server2:~/Downloads$ pwd
/home/urgen/Downloads

# curl -sS https://getcomposer.org/installer | php   

###move the file composer.phar (rename to composer) to   /usr/local/bin/composer

#mv composer.phar /usr/local/bin/composer

#change directory to your web root eg. /var/www/html/

# create project called  laravel  
#composer create-project laravel/laravel    laravel 


root@server2:/var/www/html/laravel# pwd
/var/www/html/laravel

chgrp -R www-data /var/www/html/laravel/app/storage

chmod -R 775 /var/www/html/laravel/app/storage 

goto your browser and type
http://<ip addr or localhost>/laravel/public/