Using Cron As An SSH Alternative

For someone used to the command line, using SSH is the easiest and fastest way to get things done for a client. This includes dumping a database, loading a database from a dump, moving files, creating an archive, ... etc.

The need for SSH

At 2bits, we always insist that a client has SSH enabled before we do any work. In reality, some on shared hosting cannot get it because their hosting company do not have it for their plan, or for other reasons.

This leaves us in a frustrating situation sometimes, and we have to find alternatives to do certain things.

cPanel and Plesk Are Convenient

If the client has cPanel or Plesk, things are often not so bad, since they are familiar and standard interfaces. Between cPanel's File Manager and phpMyAdmin, we can accomplish most things we need.

For example, if a client is asking for an upgrade of their Drupal install, we want to get a copy of all the files as well a database dump. The files can be done via most FTP clients using the recurse directories options. Database dumps can be done in phpMyAdmin, if the database is not too big.

Big Database Dumps

However, sometimes a database is too big. Or, after you convert the site on the test server, you cannot paste the SQL back in phpMyAdmin. We faced that situation a few days ago. We were able to dump the database to perform the upgrade on it easily from phpMyAdmin. However, trying to copy the database back using phpMyAdmin failed. Although phpMyAdmin says it allows files up to 50 MB to be uploaded, we still got a cryptic error saying "no DROP DATABASE permission" (although this permission is available, and the dump file had no DROP DATABASE statements in it).

We tried to cut the database down in size by deleting all rows from the search_total and search_index tables before uploading it in phpMyAdmin, but that did not help either. The intent was to use admin/settings/search to Reindex the site and run cron.php several times.

Shell Scripts And Cron

So, the solution we came up with was to write a shell script and execute it from cron.

First, we uploaded an uncompressed database dump file to the site.

Then we uploaded the following shell script (named it do.sh for example), and set the file mode to 755 using FTP.

After that, we accessed cPanel and set a cron job to execute the file. The command line would be /home/user/do.sh and the minute was 2 minutes after present.

#!/bin/sh
# Replace the following 3 lines with the site specific values
DB_USER=user
DB_PASSWD=password
DB_NAME=db
DUMP=/home/user/dbdump.sql
/usr/bin/mysql -u$DB_USER -p$DB_PASSWD $DB_NAME < $DUMP

Wait for a few minutes, and keep refreshing phpMyAdmin, and you will see the tables created.

Make sure that you delete the cron job after you are done.

Contents: