[英]Automating git push and commit in a shell script on a Centos server
我正在實現一個 shell 腳本,它將備份數據庫,然后將sql
文件推送到 Github,我使用的是/opt/server-scripts/backup.sh
服務器,該項目位於/opt/server-scripts/backup.sh
。 我如何自動執行此操作?
到目前為止,這是我的實現:
#!/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
您可以檢查命令/可執行文件是否已安裝或在您的 PATH 中,一種使用type
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 中是否存在程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.