Sunday, June 10, 2012

Android Reverse USB Tethering

Android usb tethering is a great feature provided in my android but sometime I feel the need to have reverse tethering. Using reverse tethering I can use my system internet on android phone via usb while my phone getting charged. It certainly helps in situations where there is a restricted access available on wifi and you don't have enough data usage or mobile network coverage on your phone like in my office. I did it on my Centos Linux box with Galaxy S but this should work with any other linux os and android phone having firmware with usb tether option.

Note: This can only be done if your phone is rooted, which may also cause to void your manufacturer warranty. Please don't follow if you don't know what you are doing.

 1. First of all enable the tethering on your phone. Settings > Wireless and network > more >Tethering & Portable > Check USB Tethering option. There should be USB0 interface appear on your system as soon as do this. 

root  ~ : ifconfig -a usb0
usb0      Link encap:Ethernet  HWaddr 02:07:30:34:35:3D 
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::7:30ff:fe34:353d/64 Scope:Link
          RX packets:13 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1200 (1.1 KiB)  TX bytes:7095 (6.9 KiB)

2. Note the IP address ( of the device interface usb0 from the system using above command.

3. Now get the IP address from the device. Login to device using android adb tool(provided with android sdk).

Note: You may have to enable the root access to adb from the developer option settings. 

root  ~ : adb shell
shell@android:/ $ su
shell@android:/ # netcfg
lo       UP                            0x00000049 00:00:00:00:00:00
svnet0   UP                              0x000000d1 a0:00:00:00:00:00
ifb0     DOWN                            0x00000082 c6:cd:01:c2:8f:40
ifb1     DOWN                            0x00000082 aa:37:52:c1:b9:ad
sit0     DOWN                            0x00000080 00:00:00:00:00:00
ip6tnl0  DOWN                            0x00000080 00:00:00:00:00:00
wlan0    DOWN                            0x00001002 b4:07:f9:73:46:d1
rndis0   UP                      0x00001043 4a:35:0f:bc:01:19

Note down the IP address of the device (

4. Check the connectivity of both the interfaces. Ping from device to system and system to device. 

shell@android:/ # ping
PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=64 time=1.36 ms
64 bytes from icmp_seq=2 ttl=64 time=0.779 ms
64 bytes from icmp_seq=3 ttl=64 time=0.705 ms

root  ~ : ping
PING ( 56(84) bytes of data.
64 bytes from icmp_seq=1 ttl=64 time=1.16 ms
64 bytes from icmp_seq=2 ttl=64 time=0.812 ms
64 bytes from icmp_seq=3 ttl=64 time=0.791 ms

5. Add system as the default gateway for the android.
shell@android:/ # route add default gw dev rndis0

6. Add the dns server setting to the device.
shell@android:/data # setprop net.dns1
shell@android:/data # setprop net.dns1

7. Enable the port forwarding on the system. 
root  ~ : echo 1 > /proc/sys/net/ipv4/ip_forward

8. Add the masquerade setting in the system firewall and map this with system's lan interface eth0 or em1( what ever the name that are using in your system. )

root  ~ : iptables --flush -t nat
root  ~ : iptables --table nat --append POSTROUTING --out-interface em1 -j MASQUERADE
root  ~ : iptables --append FORWARD --in-interface usb0 -j ACCEPT 
9. Check the net connectivity from the device using ping and other applications. 
shell@android:/data # ping                                         
PING ( 56(84) bytes of data.
64 bytes from ( icmp_seq=1 ttl=42 time=321 ms
64 bytes from ( icmp_seq=2 ttl=42 time=333 ms
64 bytes from ( icmp_seq=3 ttl=42 time=315 ms

1. It will not show any tethering connection icon in notification bar like 3G or wifi.

2. The routing done on the device is not permanently saved on the device. Once disconnected from the system, you'll have to repeat the steps on the device to enable it again.

Tuesday, June 5, 2012

MySQL Database Incremental Backup Implementation

It is the most common requirement for those who use big mysql databases in their production. Most of the people generally take the full dump of the mysql db once a week or daily. But the disadvantage of taking full dump is it may take long time to backup and restore. Also if the backups are incremental and compressed then it takes small amount of data to be transferred to backup site.

The best backup strategy is to use the full and incremental backups. The incremental backups should have short time backup frequency ( eg. 5 or 6 hours ) and once a week is enough for the full backup. It can be done with the help of mysql utility program which works for either of the mysql database engines ( eg. MYISAM and innodb ). 


The key to implement the incremental backup is to use the mysql 'binary logs'. We can configure the the mysql to generate the binary log files which contains the mysql db statements needed to update the db since the last incremental or the full backup.
The full db backup can taken with the help of mysqldump utility.

Utilities used

* mysqladmin & mysqldump comes with mysql-client package.
* mysqlbinlog comes with mysql-server package.
* Any command line FTP client program to copy the backup logs to ftp server. 

Backup Plan

1. Firstly generate the complete backup of the dbs with utility mysqldump. Compress this full backup and store this on a remote ftp storage.
2. Enable the binary logging and start storing the current binary logs which contains every mysql statement which modifies the db after the full backup.
3. Configure the binary logs in such a way that every (5 or 6 hour) the current binary log file is closed and ftp'd to the backup storage and a new binary log file is created to record the changes in the db.
4. Automate the step 3. once a week, create full backup and cleanup the previous full backup and now-obsolete binary logs from the FTP storage server.

Restore Plan

1. Copy the full backup and all the binary log files to the db server. Restore the full backup after creating the db if the db do not exists.
2. Restore the incremental binary files consecutively using the utility programs "mysqladmin" and "mysqlbinlog". The db is now uptodate to the moment of last incremental backup.
3. After complete restoration, once again take the full backup of the entire db.


1. Binary logging is configured in the mysql server.

vim /etc/mysql/my.cnf
add the folling line:

"binlog_do_db" can be used to specify the dbs for which we want to enable the binary logging. If we use this statement alone then it means all rest of the dbs which are not specified will have binary logging turned off. We can specify the multiple dbs using this statement by adding repeated enties, like:

binlog_do_db = db1
binlog_do_db = db2

"binlog_ignore_db" can also be alternatively used to specify the dbs, for which we do not want to turn on the binary logging. We can specify the multiple dbs using multiple statements like previous example.

Note: We should use the above statements very carefully because they can dangerous in certain situations. The working these statements will be explained in next post.

Restart the mysql:
/etc/init.d/mysqld restart