0

I am implementing a shell script that will be backing up the database and then push the sql file to Github, I am using centos server the project is located at /opt/server-scripts/backup.sh. How do I automate this?

Here is my implementation so far:

#!/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
Philip Mutua
  • 6,016
  • 12
  • 41
  • 84

1 Answers1

0

You can check if a command/executable is installed or it is with in your PATH, one way using type

if type -P git >/dev/null; then
  echo 'git is installed.'
fi

If you want to negate the result add the !

if ! type -P git >/dev/null; then
  echo 'git is not installed.'
fi

To add that to your script.

#!/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
  • You can add the date directly git commit -m "backup: '$(date +"%D %T")'" that way the date will be same with the output of git log
  • Other ways to check if a command exists is via command and hash see Howto check if a program exists in my PATH
Jetchisel
  • 7,493
  • 2
  • 19
  • 18