【发布时间】:2014-04-29 19:57:16
【问题描述】:
我是初学者。
这是我创建的驻留在生产服务器上的 shell 脚本。目标是使用在登台/测试服务器上找到的内容刷新我的生产 Drupal 内容。
它是如何工作的: 它进入测试/登台服务器,对我的 Drupal 内容数据库执行 mysqldump,然后将 SCP 转储到生产中的临时文件夹。从那里,它会备份当前的生产 Drupal 内容数据库(再次通过 mysqldump),然后使用来自登台服务器的转储刷新生产内容数据库。
我的问题: 虽然我知道如何编写脚本来执行命令,但我不清楚如何在下一个命令执行之前实现错误检查(例如,检查我猜的返回码?)。
#!/bin/bash
#
# Name: drupal_dump.sh
# Developer: Michael Paul
# Created: 2014-03-21
# Last updated: 2014-04-28
# Description:
# 1. Remotes into Test server, runs a script to take a dump of Drupal content DB.
# 2. Copies (scp) that dump to Prod
# 3. Takes backup of current prod Drupal content DB
# 4. Overwrites current prod Drupal content DB with the one from Test
#
# If needed, issue the following command to restore the production backup:
# mysql -p -u root myDrupalDatabase < path/to/backupfile.sql
REMOTEHOST=xx.xxx.xx.xxx
REMOTEUSER=myRemoteUsername
DUMPDIR=~/sqldumps
DESTDIR=~/incoming
BACKUPSDIR=~/backups
LOGSDIR=~/logs
TIMESTAMP="`date '+%Y%m%d_%H%M%S'`"
DUMPFILE="mysqldump_stage_${TIMESTAMP}.sql"
PRODBACKUPFILE="mysqldump_prod_${TIMESTAMP}.sql"
# Remote into Test server, backup Drupal database
echo "Creating dump file on Test server..."
ssh -i ~/.ssh/myRemoteServer.pem $REMOTEUSER@$REMOTEHOST "mysqldump --routines -u root --password= myDrupalDatabase > $DUMPDIR/$DUMPFILE && exit"
echo "Dump file created at $DUMPDIR/$DUMPFILE"
# Copy that dump to Prod
echo "Copying Test dump file to $DESTDIR on Prod..."
scp -i ~/.ssh/myRemoteServer.pem $REMOTEUSER@$REMOTEHOST:$DUMPDIR/$DUMPFILE $DESTDIR
echo "File has been copied to $DESTDIR/$DUMPFILE on Prod."
# Backup current Prod Drupal content DB
echo "Backing up current Production Drupal content database..."
mysqldump --routines -u root --password= myDrupalDatabase > $BACKUPSDIR/$PRODBACKUPFILE
echo "File backed up as $BACKUPSDIR/$PRODBACKUPFILE"
# Overwrite current Prod Drupal content DB with that taken from Test
echo "Updating Production database with content from Test..."
mysql -p -u root --password= myDrupalDatabase < $DESTDIR/$DUMPFILE
echo "Update complete."
【问题讨论】:
-
这个问题似乎跑题了,因为它是关于代码审查的。
标签: bash shell error-handling scripting