2

尝试在 Rackspace 站点上使用 Perl cron 作业备份我们的数据库。

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
4

2 回答 2

3

问题是您正在混合 Perl(您的时间戳代码)和 Shell Script(您的示例代码)......

如果要执行 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

于 2013-04-01T20:40:21.620 回答
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 脚本

于 2013-04-01T20:42:27.753 回答