ReSync MySQL Master Slave

At the master:

RESET MASTER;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

And copy the values of the result of the last command somewhere.

Wihtout closing the connection to the client (because it would release the read lock) issue the command to get a dump of the master:

mysqldump -uroot -p –all-database > /a/path/mysqldump.sql

Now you can release the lock, even if the dump hasn’t end. To do it perform the following command in the mysql client:

UNLOCK TABLES;

Now copy the dump file to the slave using scp or your preferred tool.

At the slave:

Open a connection to mysql and type:

STOP SLAVE;

Load master’s data dump with this console command:

mysql -uroot -p < mysqldump.sql

Sync slave and master logs:

RESET SLAVE;
CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000001′, MASTER_LOG_POS=98;

Where the values of the above fields are the ones you got from the folllowing:

zcat dumpfile.sql.gz | head −30 | grep -i change

Finally type

START SLAVE;

And to check that everything is working again, if you type

SHOW SLAVE STATUS;

Posted in Linux, MySQL | Comments Off on ReSync MySQL Master Slave

Unique hits to apache site

cat access.log | awk ‘{print $1}’ | sort | uniq -c | sort -g

Posted in Linux | Comments Off on Unique hits to apache site

mysqlbinlog command

The “mysqlbinlog” command reads the binary logs and outputs them in SQL format. As a result, you can use it to create a database dump and then modify that file by hand. For example, let’s say that everything since April 10th, 10AM server time needs to be restored (leaving out one statement). We would first use this command:

mysqlbinlog –start-datetime=”2013-04-10 10:0:00″ /var/log/mysql/bin.123456 > output.sql

to create the database dump. After this file is created, we can delete the offending statement that we would not want re-run. Alternately, if you know the specific binary log position, you can use the –start-position flag. Here are some articles on this process:

http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery.html
http://dev.mysql.com/doc/refman/5.5/en/point-in-time-recovery-times.html
http://dev.mysql.com/doc/refman/5.1/en/point-in-time-recovery-positions.html

Posted in Linux | Comments Off on mysqlbinlog command

To prevent an apt package from auto updating

Using apt

you can hold a package using

sudo apt-mark hold package_name

and remove the hold with

sudo apt-mark unhold package_name

Posted in Linux | Comments Off on To prevent an apt package from auto updating

Preventing access to a website from specific IP addresses with Deny/Allow

Configure the Virtual Host as follows:


Order Deny,Allow
Deny from all
Allow from 11.211.0.0/15
Allow from 12.212.0.0/15
Require valid-user
Satisfy all
AuthName “Restricted Area”
AuthType Basic
AuthUserFile /home/web/.htpasswd
Require valid-user

Create file with the following command:

htpaddwd /home/web/.htpasswd

Another option:

1. Type the following text into your VirtualHost file:

Order Allow,Deny
Allow from all
Deny from [Enter IP address here]

2. Save the file.

Alternatively, you may specify the directory by using:

Order Allow,Deny
Allow from all
Deny from [Enter IP address here]

Posted in Linux | Comments Off on Preventing access to a website from specific IP addresses with Deny/Allow

Password Protecting a web site

Add the following to .htaccess:

CentOS:

AuthType Basic
AuthName “Restricted”
AuthUserFile /etc/httpd/htaccess-pass
Require valid-user
#Order deny,allow
#Deny from All
#Satisfy any

Create the file as follows:

htpasswd /etc/httpd/htaccess-pass username
You will then be prompted for a password

Ubuntu:

AuthType Basic
AuthName “Restricted”
AuthUserFile /etc/apache2/htaccess-pass
Require valid-user
#Order deny,allow
#Deny from All
#Satisfy any

Create the file as follows:

htpasswd /etc/apache2/htaccess-pass username
You will then be prompted for a password

 

Posted in Linux | Comments Off on Password Protecting a web site

Useful netstat commands

To show which IP addresses are currently connected to your server:
netstat -nt

Posted in Linux | Comments Off on Useful netstat commands

Connecting to an external MySQL server through an SSH tunnel

Scenario: You’re at home, and you want to connect to a mysql server on the other side of a firewall. There is a machine with ssh open on it that you can use as a gateway.

  1. On your home machine:
    ssh -L 3307:domain.name.of.mysqlserver:3306 username@domain.name.of.gatewayserver

     

    This will open a tunnel, listening on localhost:3307 and forwarding everything to mysqlserver:3306, and doing it all via the ssh service on the gateway machine.

    This example shows us specifying port 3307 on the local end of the tunnel; I did this because I run a MySQL server on my home machine, so I can’t re-use the default MySQL port.

    You’ll now have a terminal open on the gateway machine, but you don’t need it for this procedure, so set it aside.

     

  2. Now, on your local machine, execute a mysql connection like so:
    mysql -u username -p -h 127.0.0.1 -P 3307 databasename

    In other words, mysql thinks it’s connecting to localhost, but on a different port. In fact, the connection is being made securely to the remote mysql server, via the gateway machine and the local “mouth” of the ssh tunnel on your own machine. 

  3. When you’re finished with your mysql session, log out of the session on the gateway machine. That will properly close the tunnel.
Posted in Linux, SSH | Comments Off on Connecting to an external MySQL server through an SSH tunnel

Rsync Commands

rsync -avnh source target

#Use rsync to sync to remote system as user over remote shell compressing transfer and without crossing filesystem boundaries
rsync -avzx -e ssh /var/www/ user@remote_host:/var/www/

#Use rsync to sync to remote system as user over remote shell skipping files that are newer on the remote server
rsync -avzxu -e ssh /var/www/ user@remote_host:/var/www/

Pushing files with rsync
rsync -a ~/dir1 user@remote_host:/var/www/

Pulling files with rsync
rsync -a user@remote_host:/home/user/dir1 /var/www

#To see which files need to be synced:
rsync -n –dry-run -av /var/www/vhosts/domain.com/. remote_host:/var/www/vhosts/domain.com/.

 

-a = archive which basically means use recursion and preserve symbolic links, devices, attributes, permissions, ownerships, etc.(not hard links)
-v = verbose
-z = compression
-n = dry run
-h = human readable
-P = progress bar for the transfers and allows you to resume interrupted transfers
-u = skip files that are newer on the receiver
–delete option allows deletions from destination if not in source
–dry-run
–exclude=pattern_to_exclude excludes items matching the pattern (Good practice to run with –dry-run first to be sure)

* By default, rsync does not delete anything from the destination directory.
 

Posted in Linux | Comments Off on Rsync Commands

Using grub

It is relatively easy to boot GNU/Linux from GRUB, because it somewhat resembles to boot a Multiboot-compliant OS.

Set GRUB’s root device to the same drive as GNU/Linux’s. The command search –set=root –file /vmlinuz or similar may help you (see search).
Load the kernel using the command linux (see linux):

grub> linux /vmlinuz root=/dev/sda1

If you need to specify some kernel parameters, just append them to the command. For example, to set acpi to ‘off’, do this:

grub> linux /vmlinuz root=/dev/sda1 acpi=off

See the documentation in the Linux source tree for complete information on the available options.

With linux GRUB uses 32-bit protocol. Some BIOS services like APM or EDD aren’t available with this protocol. In this case you need to use linux16

grub> linux16 /vmlinuz root=/dev/sda1 acpi=off

If you use an initrd, execute the command initrd (see initrd) after linux:

grub> initrd /initrd

If you used linux16 you need to use initrd16:

grub> initrd16 /initrd

Finally, run the command boot (see boot).

**Totally stolen from http://www.gnu.org/software/grub/manual/grub.html#Loading-an-operating-system-directly

Posted in Linux | Comments Off on Using grub