Saturday, October 27, 2012

Run a script at logout/shutdown/restart on ubuntu


If the script name is script.sh, then do the following steps and also make sure that the script is executable :

sudo cp script.sh /etc/init.d
sudo ln -s /etc/init.d/script.sh /etc/rc0.d/K10script.sh
sudo ln -s /etc/init.d/script.sh.sh /etc/rc6.d/K10script.sh

Friday, October 26, 2012

Backup MySQL : Execute database query using PHP file.


Below is an example of using SELECT INTO OUTFILE query for creating table backup 

<?php
$dbhost = '';
$dbuser = ' ';
$dbpass = ' ';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'db_name';
mysql_select_db($dbname);
$tableName  = 'table_name';
$backupFile = '/backup/file_name.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query) or die();
mysql_close($conn);
}
?>

To restore the backup you just need to run LOAD DATA INFILE query like this :

<?
 $tableName  = 'table_name';
$backupFile = 'file.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysql_query($query);
?>

Facing Problem :

when you try to create output file on server may be faced 
Error : Can't create/write to file '/var/www/test/backup/mypets.sql' (Errcode: 13)

Solution :

Recent Ubuntu Server Editions (such as 10.04) ship with AppArmor and MySQL's profile might be in enforcing mode by default. You can check this by executing sudo aa-status like so:

# sudo aa-status
5 profiles are loaded.
5 profiles are in enforce mode.
   /usr/lib/connman/scripts/dhclient-script
   /sbin/dhclient3
   /usr/sbin/tcpdump
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/sbin/mysqld
0 profiles are in complain mode.
1 processes have profiles defined.
1 processes are in enforce mode :
   /usr/sbin/mysqld (1089)
0 processes are in complain mode.
If mysqld is included in enforce mode, then it is the one probably denying the write. Entries would also be written in /var/log/messages when AppArmor blocks the writes/accesses. What you can do is edit /etc/apparmor.d/usr.sbin.mysqld and add /backup/ or /data/* near the bottom like so:

...
/usr/sbin/mysqld {
    ...
    /var/log/mysql/ r,
    /var/log/mysql/* rw,
    /var/run/mysqld/mysqld.pid w,
    /var/run/mysqld/mysqld.sock w,
    /data/ r,
    /data/* rw,
   /backup/* rw,
}

And then make AppArmor reload the profiles.

# sudo /etc/init.d/apparmor reload
WARNING: the change above will allow MySQL to read and write to the /data directory. We hope you've already considered the security implications of this.



Tuesday, May 29, 2012

rsync Command

rsync is used to perform the backup operation in UNIX / Linux. rsync utility is used to synchronize the files and directories from one location to another in an effective way.  

Example : Synchronize Files From Local to Remote using shell(ssh with port)
rsync -avzi -e "ssh -p 2445" /var/lib/ldap/ username@192.168.32.101:/var/lib/ldap/

Example : Synchronize Files From Remote to Local

rsync -avz username@192.168.200.10:/var/lib/rpm /root/temp

Read More : Click here 

Saturday, May 19, 2012

Filter mail with postfix header_checks


First up under /etc/postfix ensure that you have a file called header_checks. If not create it.
Next we want to ensure Postfix is configured to use this file so you do this from a command line by entering the following:
postconf -e "header_checks = regexp:/etc/postfix/header_checks"
The syntax in the header_checks file is:
/regex_pattern/ ACTION
Example :
#/etc/postfix/header_checks

/^From: "spam/ REJECT

/^To: example@example.com/ REDIRECT @esxe.com

/^Subject:.*viagra/ DISCARD
More Detail Link 1 Link 2

PHP : mysql_real_escape_string() function


The mysql_real_escape_string() function escapes special characters in a string for use in an SQL statement
The following characters are affected
Syntax : mysql_real_escape_string(string,connection)
string : Required. Specifies the string to be escaped
connection  : Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.

Example :
<?php
// Connect$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')    OR die(mysql_error());
// Query$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),      
           mysql_real_escape_string($password));
?>

Sunday, February 19, 2012

Perform SSH Login Without Password

 Perform SSH Login Without Password Using only 3 Commands.

 $ssh-keygen
 $ssh-copy-id -i ~/.ssh/id_rsa.pub remote_host
 $ssh remote_host

More Detail Click Here