簡體   English   中英

Perl腳本來備份Rackspace上的數據庫

[英]Perl script to backup database on Rackspace

嘗試在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 Script(您的示例代碼)...

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM