我正在实现一个shell脚本,该脚本将备份数据库,然后将sql
文件推送到Github,我使用的是位于/opt/server-scripts/backup.sh
的centos服务器。如何实现自动化?
以下是我到目前为止的实现:
#!/bin/bash/
var=$CURRENT_DATE=date +"%D %T"
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
# Git Push
GIT=$(which git)
REPO_DIR=/opt/server-scripts/
cd ${REPO_DIR} || exit
${GIT} add --all .
${GIT} commit -m "backup:" + "'$CURRENT_DATE'"
${GIT} https://pmutua:xxxxx@github.com/pmutua/sqlbackup.git master
您可以使用type
检查命令/可执行文件是否已安装或在您的PATH中
if type -P git >/dev/null; then
echo 'git is installed.'
fi
如果要否定结果,请添加!
if ! type -P git >/dev/null; then
echo 'git is not installed.'
fi
将其添加到脚本中。
#!/usr/bin/env bash
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
if ! type -P git >/dev/null; then ##: Check if git is not installed
echo 'git is not installed.' >&2 ##: print an error message to stderr
exit 1 ##: Exit with an error
fi
# Git Push
CURRENT_DATE=$(date +"%D %T") ##: Assign the output of date in a variable
REPO_DIR=/opt/server-scripts/
cd "${REPO_DIR}" || exit
git add --all .
git commit -m "backup: '$CURRENT_DATE'"
git push https://pmutua:xxxxx@github.com/pmutua/sqlbackup.git master
- 您可以直接添加日期
git commit -m "backup: '$(date +"%D %T")'"
这样date
将与git log
的输出相同 - 检查命令是否存在的其他方法是通过
command
和hash
查看如何检查程序是否存在于我的PATH中