簡體   English   中英

在 Centos 服務器上的 shell 腳本中自動化 git push 和 commit

[英]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的輸出相同
  • 檢查命令是否存在的其他方法是通過commandhash請參閱如何檢查我的 PATH 中是否存在程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM