简体   繁体   中英

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

The problem is that you're mixing Perl (your timestamp code) and Shell Script (your sample code)...

You can use backticks if you want to execute the shell code:

#!/usr/bin/perl <---- or whatever is your path to perl
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;`

Besides backticks, you can also use system() or exec

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM