Search This Blog

Tuesday, March 27, 2012

Making a mysql back up every day midnight using cron

This is a simple way to back up your database everyday Go to ubuntu command line and log in as root user, type following command to open crontab file

 >crontab -e

 type the following command in the file

 0 0 * * * mysqldump -uYOURUSER -pYOURPASSWORD YOURDBNAME > /home/tommy/my_back_up/mydb_`date +\%y-\%m-\%d`.sql

 you can restart cron by typing

 >restart cron

 OR

 >/etc/init.d/cron restart

This will create a sql dump file everyday midnight
file name would be mydb_2012-04-21.sql

Wednesday, March 7, 2012


/**
     * Converts minutes to hours
     * @param type $mins
     * @return string 
     */
    public static function m2h($mins) {
        if ($mins < 0) {
            $min = Abs($mins);
        } else {
            $min = $mins;
        }
        $H = Floor($min / 60);
        $M = ($min - ($H * 60)) / 100;
        $hours = $H + $M;
        if ($mins < 0) {
            $hours = $hours * (-1);
        }
        $expl = explode(".", $hours);
        $H = $expl[0];
        if (empty($expl[1])) {
            $expl[1] = 00;
        }
        $M = $expl[1];
        if (strlen($M) < 2) {
            $M = $M . 0;
        }
        $hours = $H . "." . $M;
        return $hours;
    }

Tuesday, March 6, 2012

PHP function to return an array representation of calender month

If you need an array representation of a calender month this function will come in handy. This function will return an array of a given month with days properly distributed in to weeks, as in real calendar.

public function buildMonthCalendar($year, $month) {

        $calendar = array(
            'week-1' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-2' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-3' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-4' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-5' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-6' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
        );

        $startOfMonth = "{$year}-{$month}-01";
        $result = strtotime("{$year}-{$month}-01");
        $endOfMonth =  date('Y-m-d', strtotime( date('Y-m-d', strtotime( $result . '+1 month')).' -1 second'));

        $j = 1;
        for ($i = $startOfMonth; $i != $endOfMonth;) {

            $weekDay = date('D', strtotime($i));
            $calendar["week-{$j}"][$weekDay] = date('d', strtotime($i));
            if ($weekDay == 'Sun') {
                $j++; //jump to next week
            }
            $i = date('Y-m-d', strtotime($i . ' +1 day'));
        }

        $weekDay = date('D', strtotime($endOfMonth)); //last day of the month
        $calendar["week-{$j}"][$weekDay] = date('d', strtotime($endOfMonth));

        return $calendar;
    }


Change color of table column using jquery

If the table id is punchData and if you are changing second column color
$("#punchData > tbody > tr > td:nth-child(2)").css("background","#F5D0A9");

Tuesday, February 21, 2012

list all cron jobs for all users

for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done

Thursday, January 12, 2012

Select a lable using "for" in JQuery

Jquery selector for the label of a checkbox $("label[for=genTaxRateOrder]").

Difference of self and static in inheritance, php example


class A {
    public static function get_A() {
        return new self();
    }
    public static function get_me() {
        return new static();
    }
}

class B extends A {

}

echo get_class(B::get_A());  // out put A
echo get_class(B::get_me()); // out put B
echo get_class(A::get_me()); // out put A

Wednesday, January 4, 2012

Zend Framework db examine SQL query Or Print SQL query

$db->getProfiler()->setEnabled(true);
$db->update($data, array('id = ?' => $Posts->id));
print $db->getProfiler()->getLastQueryProfile()->getQuery();
print_r($db->getProfiler()->getLastQueryProfile()->getQueryParams());
$db->getProfiler()->setEnabled(false);

Thursday, December 8, 2011

How to do a “git export” From Command Line


git archive

git archive master | tar -x -C /somewhere/else

If you want a compressed archive .

git archive master | bzip2 >source-tree.tar.bz2

ZIP archive

git archive --format zip --output /full/path/to/zipfile.zip master

Thursday, December 1, 2011

Setting Up Name Based Virtual Hosting In Ubuntu

1. Append the following line to your /etc/apache2/apache2.conf .
     NameVirtualHost 127.0.0.2:80
2 . Create unique files for each of my domains within the /etc/apache2/sites-available/ folder.Create a fie called mysitename.com

<VirtualHost 127.0.0.2:80>
ServerName 
mysitename.com
ServerAlias www.
mysitename.com
ServerAdmin me@ubuntu.com
DocumentRoot /var/www/mysitename/html
</VirtualHost>


3. Enable it by creating a symbolic link from one folder to the next ( etc/apache2/sites-enabled/)

sudo a2ensite mysitename.com ( to diable it anytime sudo a2dissite  mysitename.com)  

harder way to create the symbolic link 

cd /etc/apache2/sites-enabled/
ln -s ../sites-available/
mysitename.com .


4. Editing the /etc/hosts by adding the following line

127.0.0.2:80 mysitename.com 

5. Restart apache sudo /etc/init.d/apache2 restart

****

To enable mod_rewrite in Ubuntu, you just need to write this command in terminal

sudo a2enmod rewrite