脚本使用其数据库备份网站

时间:2012-08-24 16:08:42

标签: sql linux bash web

这是代码,想法是在阵列中建立的日子里备份一个站点(在这个测试中我已经放了7天):

#!/bin/bash

### Setup Environment ###
DIRS="INFO"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
DAYSOFBACKUP=( Mon Tue Wed Thu Fri Sat Sun)

### MySQL Config ###
MUSER="INFO"
MPASS="INFO"
MHOST="INFO"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

### FTP Config ###
FTPD="/"
FTPU="INFO"
FTPP="INFO"
FTPS="INFO"

### Email Config ###
EMAILID="INFO"

### FS Backup ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :

### Determine which backup to run ###
for day in ${DAYSOFBACKUP[@]}
do
  if [ $day == "$FULLBACKUP" ]; then
    i=$(date +"%Hh%Mm%Ss")
    FTPD="/full"
    FILE="fs-full-$NOW.tar.gz"
    tar -zcvf $BACKUP/$FILE $DIRS
  else
    i=$(date +"%Hh%Mm%Ss")
    FILE="fs-i-$NOW-$i.tar.gz"
    sudo tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
  fi
done

### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
if [ db == "bricalia_tienda" ]; then
 FILE=$BACKUP/mysql-$db.$NOW-$i.gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
fi
done

### FTP Backup to Remote Server ###
#Start FTP backup using ncftp
ftp $FTPS $MUSER $MPASS
bin
sudo mkdir $FTPD
sudo mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit

### Backup Fail Check ###
if [ "$?" == "0" ]; then
 rm -f $BACKUP/*
else
 T=/tmp/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "Backup failed" >>$T
 mail  -s "BACKUP FAILED" "$EMAILID" <$T
 rm -f $T
fi

我在&#34中有错误;确定在if子句&#34;中运行哪个备份。它在最后一行也有问题:意外结束。

此处解释了脚本:http://piratecove.org/website-backup-script/

2 个答案:

答案 0 :(得分:2)

在您引用的评论下方,看起来是一个重复的行。如果检查:

### Determine which backup to run ###
if [ "$DAY" == "$FULLBACKUP" ]; then
if [ $day == "$FULLBACKUP" ]; then

如果检查有帮助,我会猜测删除第二个。

答案 1 :(得分:1)

在“修复”之后,还有另一个问题:

### Determine which backup to run ###
for day in ${DAYSOFBACKUP[@]} do
if [ $day == "$FULLBACKUP" ]; then
  i=$(date +"%Hh%Mm%Ss")
  FTPD="/full"
  FILE="fs-full-$NOW.tar.gz"
  tar -zcvf $BACKUP/$FILE $DIRS
else
  i=$(date +"%Hh%Mm%Ss")
  FILE="fs-i-$NOW-$i.tar.gz"
  tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi

这个for循环不对,试试这个:

### Determine which backup to run ###
for day in ${DAYSOFBACKUP[@]}
do
  if [ $day == "$FULLBACKUP" ]; then
    i=$(date +"%Hh%Mm%Ss")
    FTPD="/full"
    FILE="fs-full-$NOW.tar.gz"
    tar -zcvf $BACKUP/$FILE $DIRS
  else
    i=$(date +"%Hh%Mm%Ss")
    FILE="fs-i-$NOW-$i.tar.gz"
    tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
  fi
done