Friday, July 15, 2011

Scheduled Remote MySQL backup using PHP

There's a nice 10 ways to backup mysql database from here.

There's also the use of MySQL Administrator or MySQL Workbench if both machines are Windows-based.

However, if you have the MySQL remotely and can be on Linux; some are on Windows; on different machines; and you want to grab backup from all of them, this one can be helpful.

On my development desktop, I have PHP and MySQL installed. Though it is not necessary to have these installed, it's rather handy on a development workstation.

1. Copy, paste it to your favorite text editor and save it somewhere as backup.php (or any filename you wish, just make sure it's recognized by PHP). For the sake of this tutorial, I saved it at D:\DBbackup\

<?php
/**
* This should be run on command prompt
* @params
* host
* user
* password
* [db] defaults as {host}
* [port] defaults as 3306
*/
#host,user,password,database,port
$DATABASES = array(
array(
'user' => 'user1'
,'host' => '8.8.8.8.8'
,'password' => 'mypassword'
)
,array(
'user' => 'user2'
,'host' => 'mysql.mysql.com'
,'password' => 'anotherpassword'
,'port' => '6609'
)
,array(
'user' => 'user2'
,'host' => 'host.localdomain.net'
,'password' => 'itspassword'
,'db' => 'mydatabase'
,'port' => '3308'
)
# add as many database connection as needed using the structure above
);
if (defined('STDIN')){ # make sure it's only run on command line interface (CLI)
$start = date('H:i:s');
$date = date('Ymd');
foreach($DATABASES as $instance){
$port = (isset($instance['port']) ? $instance['port'] : '3306');
$db = (isset($instance['db']) ? $instance['db'] : $instance['user']);
exec("mysqldump "
." -h".$instance['host'] #host
." -u".$instance['user'] #user
." -p".$instance['password'] #password
." -P".$port
." --compress" #you may customize the options appropriate to your servers
." --create-options"
." --extended-insert"
." --no-create-db"
." --quote-names"
." --verbose"
." ".$db
." --result-file=".dirname(__FILE__).DIRECTORY_SEPARATOR.$date."-".$db."@".$instance['host'].".sql"
);
}
# you may add other routines here like:
# compress using 7z
exec("echo 'STARTED: ".$start." DONE: ".date('H:i:s')."' > ".$date.".txt"); #keep log
} else{
echo 'This should be run on a command line';
}
?>


2. Test it on your local
a. open a command prompt on d:\dbbackup\ directory
> d:
> cd dbbackup
b. test php [PHPpath]php --info
> C:\PHP\php.exe D:\dbbackup\backup.php
c. it will create *.sql files on d:\dbbackup\ directory for every database you want to backup

3. Add as task to automate execution
a. Open Task Scheduler
b. Add new task that starts a program.
Program to execute: C:\PHP\php.exe
Parameters (or add it to Program to execute depending on your Windows version)
: D:\dbbackup\backup.php
This will basically execute as
C:\PHP\php.exe D:\dbbackup\backup.php
Put the desired frequency and schedule to backup.

The length of time to execute the script will depend on the amount of data and speed of your connection to the server.

Wednesday, February 16, 2011

JQuery Mobile Project update

I just launched a list of modules using jQueryMobile just in time for the Alpha3 release of jQueryMobile. I had employed tabs, menu-selections, form submissions.

I just yet to know more on how to execute JS routines on the modules in any way possible. I'm hoping it would be jQuery(ish) and not native JS.

It is easy and fun doing the modules. I had not to worry about layout, design and color-selections as they are already pre-built with the package. I created a templating system to make development even faster. This allowed me to produce the mobile-counterpart of submodules on the system.

Next move is to add more functionalities and open opportunities exploiting the user-experience of the mobile. A new module fo e-commerce shop will be underway.

Wednesday, February 2, 2011

Web Development for the Mobile

A new project drops heavily on my lap the past few days: develop a system that interfaces with a mobile browser primarily to support the online education project of the college.

The college has been using Moodle (http://moodle.com) as its Learning Management System (LMS) and the closest link towards the project is Mobile Moodle (MOMO http://mobilemoodle.org/).

I read through the product and it requires me to have a mobile device with web browser and preferrably with a WiFi connection. At the back of my mind, if I am to build with a web browser, there's not much options to choose from away from what I am currently doing -- web application with PHP, MySQL and JS (jQuery preferrably).

With more searches, clicks and reading, I have come accross to:

  • Sencha (http://sencha.com) - a Mobile Javascript built from ExtJS background.
  • WebKit Open Source Project (http://webkit.org) - web browser engine.
  • FMA SDK (http://fma.sourceforge.net) - had used it to interface between mobile and other appliances.
  • PhoneGap (http://phonegap.com) - open source mobile platform that supports 6 platforms. 
  • jQTouch (http://jqtouch.com) - "A jQuery plugin for mobile web development on iPhone, iPod Touch, and other forward-thinking devices". This can use the WebKit for a preview and development.
  • dotMobi (http://mtld.mobi) - This offers training for design, development and background information about mobile development.
  • Nokia SDKs (http://forum.nokia.com) - flowing through with the dotMobi leads and just about to start dwelling into learning each one of those mentioned in its training articles and archives.

Then suddenly, I just felt the urge to go ahead and start to code. I decided to make a new module - new set of libraries, objects, structure and coding standards. But in order to do that, it's nice to get acquianted with MVC platforms for a change. I crossed with Kohana (http://kohanaphp.com) -- a PHP5 MVC framework from CodeIgniter background.

I then went through a little tutorial on how to use Kohana and it seems promising. Tried a few samples but then I stopped. Starting a new one with this platform would mean I have to forget about how I got through all the codes I've used in the same project I am working with.

After a few minutes of big sighs and a mug of coffee, my click landed on jQMobile (http://jqmobile.com) - jQuery's own(as in from the creator of jQuery) mobile platform. It is on its Alpha 2 release and sounds very promising:

  • least adjustment in coding structure
  • gets me more intimate with jQuery
  • gets me more intimate with MVC framework
  • lets me grow with it

Well, a new tool for keeping me to speed, a big step in keeping me ahead.

Wish me luck!

Wednesday, January 26, 2011

PHP5.2.14 to PHP5.3.5 initial impressions on migration

I had been optimistic that my codes way back from PHP4.3 can do well with my the new version PHP5.3.5 after my codes had undergone migration to PHP5 standards.

I decided to use PHP5.3.5 after its announcement that PHP5.2 reaches its EOL (end of life) -- meaning, there's no more effort in maintaining it -- so the only path is to go with the 5.3 series.

My initial readings had lead me to some of the important issues for the migration: http://www.php.net/manual/en/migration53.php specifically on the backward incompatible changes.

I had paid less attention to the differences on PHP5. But just migrating to the new version changed all that.

A brief rundown of the issues I begin to love on the new one:

1. "strrpos() and strripos() now use the entire string as a needle" - this allows faster and better determination of the existence of a string instead of using strstr() and stristr()

2. normalization of path on Windows when using include_once() and require_once() "so that including A.php and a.php include the file just once".

3. array_replace() and array_replace_recursive() - replaces elements from passed arrays into one array. This should have been #1 on my list. Most of my functions and methods can now accept array parameters that act as triggers and options to return values. They must, however, have default values.

4. "gethostname() - Return the current host name for the local machine". Great use in adding info to the log

5. "date_add() - Adds an amount of days, months, years,hours, minutes and seconds to a DateTime object" and the DateTime class and its methods... I have to upgrade a lot of these.

Well, this is an initial list.. I may have to scour the whole repository for codes that needed upgrading and migration.

Good luck to me!

 

 

Thursday, January 20, 2011

Green - economical, minimal polution, earth-friendly

Talk about "green ideas" and a lot of people find it differently:

  • green-minded? yeah, you can guess
  • earth-friendly? in ads? in products? in ways and wastes?
  • just color them green

Yesterday, I am acquainted with something really cool. Cool that I'd like to get one right away. If only I have more reasons to get one, I should have asked someone get it for me -- without thinking how much it cost.

Yesterday, I met a few models of ELECTRIC SCOOTERS -- the kind where there's no smoke, almost ninja-move silence, almost  no-maintenance, regular-looking.. (oh I have a lot in mind to describe it away from the regular ones I know.

They are battery operated, no oils needed, no sparkplugs to clean, no Transportation Registration necessary.

They come in models that differs in wattage and a few features. Wattages range from 400 to 650, at least for the models on display and can bring you to 80-100 kilometers on a battery charge. One, you can drive manually or turbo-speed the scooter. Charging is just as simple as putting on the plug as you do with any appliance.

If you are not convinced by the basic idea that its economical, eco-friendly, easy to operate and costs less than half the price of a regular gasoline-operated scooters, the design can deceive you with the absence of the exhaust pipe.

One thing that some riders look for is the speed. These models only can max 50 kilometers per hour. Forget about the sound, it's silent as wheels on friction with the road.

I'm putting up the site http://scootersphilippines.com/ for info on how to get one.

 

Tuesday, January 18, 2011

I'm currently one of those called "Company Men"

What if one day, you got back to work, a little bit late, a litte bit in a rush, a little bit upbeat and a litte bit hasty, however, just to know within the first hour that you're out of the job?

You have worked for years to go up the so called "ladder", keep a high standard of workmanship -- dependable, independent, initiator, democratic and "loyal". Such effort turned the tide and the next thing you know, you're out of job.

"What a pathetic story" -- must have applied during the time of our forefathers -- industrial age. This time, this could be really "REAL". The older we get in a company, the better chance we get associated with "company liability".

The movie "Company Men" starts with the sad story of good (smart, idealistic and productive) people being fired. It struct me with the question posted above.

Robert Kiyosaki, author of the book "Rich Dad, Poor Dad", suggests a lot of ideas in his "Rich Kid, Smart Kid" edition of his book: don't work for money, let money work for you. It's a little absurb at first but it takes an alarm level and conscious thinking as I read the book. Now that I'm watching this movie, it sounds more bells of alarm: "What if I losses my job?"

The "Rich Kid" suggests that I have to invest in a way -- whatever and however that may be, to start.

Anyone out there want to lend a hand, drop by a comment, I'd love to get in touch with you.

Monday, January 17, 2011

GoDaddy domain transfer

When you sign up for godaddy hosting, it will try to identify the closest server farm from your location. The purpose of which is to make your access to the server faster -- less latency and DNS seeks.

However, at some point, this is condescending to the purpose of the site. You would want your server be closer to your intended audience rather than to yours.

Just today, I finished transfering some domains that are hosted on GD's Singapore server farms to their US server farms. My client is forced to get another hosting plan (extension of the offer from GD is a good reason). To transfer, you have just a few minutes to make the transfer. The transfer on a shared linux hosting entails as follows:

  1. Download webfiles to your localhost
  2. While you're downloading, create the database on the new host.
  3. Modify the config files for path and DB connection settings on your local copy
  4. Upload the files to the new host
  5. While you're doing #4, copy the database to your new host
  6. When #5 is done, delete the domain from the old host
  7. Create the domain on the new host. An error will be displayed if the domain is not yet cleared from the old host.
  8. Traceroute for the new domain to resolve to the new host. You may have to modify the DNS server.
  9. Create FTP account for the specific domain if necessary
  10. Test the new site with the configurations set

It's essential to have the fastest Internet connection available to facilitate the smooth transfer.

Should you need assistance soon, you can email aingelcarbonell_at_gmail_dot_com.