问题描述
我有一个在启动时运行的shell脚本,用于备份MAMP MySQL安装.不幸的是,如果MAMP没有运行,它将抛出一个错误并将其发送到内部邮件.我希望忽略任何错误(发送到/dev/null
),但是如果没有错误,请继续.
I have a shell script that I run in launchd that backs up my MAMP MySQL install. Unfortunately if MAMP isn't running it throws an error and sends it to internal mail. I want any errors to be ignored (sent to /dev/null
), but if there isn't an error, to continue on.
我尝试了以下操作,该操作可以在MAMP运行时正确创建备份,但是如果未运行则发送邮件(stderr):
I have attempted the following, which correctly creates the backup when MAMP is running, but sends mail if it's not running (stderr):
#!/bin/bash
/Applications/MAMP/Library/bin/mysqldump --opt -u root -pPASSWORDHERE --host=localhost --all-databases > | gzip -9 > /Users/Paul/Dropbox/Development/Shared\ DBs/BACKUPS/all_databases_-`date "+%Y-%m-%d_%H.%M.%S"`.sql.gz
我也尝试过这个:
#!/bin/bash
/Applications/MAMP/Library/bin/mysqldump --opt -u root -pPASSWORDHERE --host=localhost --all-databases 2> /dev/null 1> | gzip -9 > /Users/Paul/Dropbox/Development/Shared\ DBs/BACKUPS/all_databases_-`date "+%Y-%m-%d_%H.%M.%S"`.sql.gz
但这根本没有任何作用...
But this does nothing at all...
如果有错误,我需要:
- 要发送给
/dev/null
的错误 - 没有要创建的压缩文件
- Error to be sent to
/dev/null
- No Gzipped file to be created
如果没有错误,我需要:
If there's no error I need:
- 要创建的压缩mysqldump
有人知道我要去哪里吗?
Does anyone know where I'm going wrong?
预先感谢您的帮助.
我现在有:
#!/bin/bash
# Create the backup name
BACKUP_NAME="/Users/Paul/Dropbox/Development/Shared DBs/BACKUPS/all_databases_-$(date "+%Y-%m-%d_%H.%M.%S").sql"
# Run mysqldump and create a raw backup
/Applications/MAMP/Library/bin/mysqldump --opt -u root -proot --host=localhost --all-databases 2> /dev/null > ${BACKUP_NAME}
# If no errors
if [$? = 0]
then
# Create the gzip backup
gzip -c9 ${BACKUP_NAME} > ${BACKUP_NAME}.gz
fi
# In any cases remove raw backup
rm -f ${BACKUP_NAME}
但是即使没有错误,这也不会产生文件.
but this doesn't produce a file even when there are no errors.
使用下面的JML更新代码(非常感谢),我仍然遇到错误,但这是因为我的路径中有空格,因此需要在两个地方加上引号.
Using JML's updated code below (many thanks), I was still getting errors, but this was because my path had a space in it, therefore needed quote marks in two places.
最终有效代码:
#!/bin/sh
# Create the backup name
BACKUP_NAME="/Users/Paul/Dropbox/Development/Shared DBs/BACKUPS/all_databases_-$(date +%Y-%m-%d_%H.%M.%S).sql"
# Run mysqldump and create a raw backup
/Applications/MAMP/Library/bin/mysqldump --opt --user=root --password=root --host=localhost --all-databases 2> /dev/null >${BACKUP_NAME}
# If no errors, create the gzip backup
test $? = 0 && gzip -c9 "${BACKUP_NAME}" > ${BACKUP_NAME}.gz
# In any cases remove raw backup
rm -f "${BACKUP_NAME}"
推荐答案
为什么不拆分?像这样:
Why not splitting ? Something like this:
#!/bin/sh
# Create the backup name
BACKUP_NAME="/tmp/backup-$(date +%Y-%m-%d_%H.%M.%S).sql"
# Run mysqldump and create a raw backup
mysqldump --opt --user=root --password=root --host=localhost --all-databases 2>/dev/null >"${BACKUP_NAME}"
# If no errors, create the gzip backup
test $? = 0 && gzip -c9 "${BACKUP_NAME}" > "${BACKUP_NAME}.gz"
# In any cases remove raw backup
rm -f "${BACKUP_NAME}"
这篇关于将Stderr发送给dev/null,但是如果没有错误发生,请继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!