perlcronrackspace

Perl script to backup database on Rackspace


Trying to backup our database using Perl cron job on Rackspace Sites.

Rackspace gives an example of:

#!/bin/sh
mysqldump -h DB_HOST -u DB_USER -p'DB_PASSWORD' DB_NAME > YOUR_WEB_ROOT/db_backup.sql
gzip -f YOUR_WEB_ROOT/db_backup.sql

That works fine but I want a timestamp put into the filename. I have this bit of code that generates the timestamp:

use strict;
use warnings;
use DateTime;

my $dt   = DateTime->now;
my $date = $dt->ymd;
my $time = $dt->hms;

my $file = "****.com/db_backups/db_backup - $date $time.sql";

print $file;

The final Product:

#!/bin/sh
use strict;
use warnings;
use DateTime;

my $dt   = DateTime->now;
my $date = $dt->ymd;
my $time = $dt->hms;

my $file = "****.com/db_backups/db_backup - $date $time.sql";

print $file;

mysqldump -h hosturl -u username -p'password' database > $file;
gzip -f $file;

When it runs I get these errors.

/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 2: use: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 3: use: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 4: use: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 5: my: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 6: my: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 7: my: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 8: my: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 9: print: command not found
/mnt/stor09-wc1-ord1/758094/****.com/backup.sh: line 10: $file: ambiguous redirect

Solution

  • #!/bin/sh
    todaysdate=`date +%F`
    mysqldump -h DB_HOST -u DB_USER -p'DB_PASSWORD' DB_NAME > YOUR_WEB_ROOT/db_backup.sql
    gzip -f YOUR_WEB_ROOT/${todaysdate}db_backup.sql
    

    Fill in the DB_HOST etc like you would normally

    No perl required, this is all shell script