简体   繁体   English

Perl脚本来备份Rackspace上的数据库

[英]Perl script to backup database on Rackspace

Trying to backup our database using Perl cron job on Rackspace Sites. 尝试在Rackspace站点上使用Perl cron作业备份我们的数据库。

Rackspace gives an example of: 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

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)... 问题是您正在混合Perl(您的时间戳记代码)和Shell Script(您的示例代码)...

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 除了反引号,您还可以使用system()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 像往常一样填写DB_HOST等

No perl required, this is all shell script 不需要perl,这就是所有的shell脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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