尝试使用Rackspace Sites上的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
答案 0 :(得分: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
答案 1 :(得分: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脚本