2013-04-01 72 views
2

嘗試在Rackspace站點上使用Perl cron作業備份我們的數據庫。用於在Rackspace上備份數據庫的Perl腳本

給出了Rackspace公司的例子:

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

這工作正常,但我想投入的文件名時間戳。我有這段代碼生成時間戳:

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; 

最終產品:

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

當它運行時我得到這些錯誤。

/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 

回答

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

像你一樣填寫DB_HOST等

不需要perl,這是所有的shell腳本

3

的問題是,你的Perl混合(您的時間戳代碼)和Shell腳本(示例代碼)...

如果要執行shell代碼,你可以使用反引號:

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

而且反引號,你也可以使用system()exec