gpt4 book ai didi

用于在 Rackspace 上备份数据库的 Perl 脚本

转载 作者:行者123 更新时间:2023-12-02 05:09:17 26 4
gpt4 key购买 nike

尝试在 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

最佳答案

问题是您混合使用了 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

关于用于在 Rackspace 上备份数据库的 Perl 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com