Bash Scripting for auto-commit and push GIT

Dwi Misbachul Munir
4 min readMar 25, 2022

Hi everyone, today I learn something about bash scripting, wherein in my case I want to create an auto-commit and push my database backup to the Gitlab repository. I have consulted with my friends and they are suggesting using a bash script.

before, I don't know what is script bash and how I can use it to solve my case. finally, after I search some articles on google and asked my friends about this, I can solve my case.

On this chance, I will share my case with you are guys, I hope from my case you can get some knowledge. okay to the point let's started

first, you must add ssh public key-gen in your local computer to GitLab or GitHub, you can see tutorials on youtube or google.

second, after adding an ssh key to your GitLab account. create an auto-git.sh file in your root project

open ‘auto-git.sh’ in visual studio code or another code editor. before we are writing some code in this file we must know the location of bash in the computer system, open your terminal, and type which bash

after we know where is bash location, move to visual studio code, and let's write a script command

in the first write like this

#!/usr/bin/bash

Every bash shell script in this tutorial starts with a shebang: #! which is not read as a comment. A first line is also a place where you put your interpreter which is in this case: /usr/bin/bash.

the second line is command bash to dump/export my database to a file output_file_name.sql

in the next line add git checkout main. you must already know what this is for? yup, this is to switch to branch main. you can change main to another branch as you want.

why is every command written in the new line? because bash script read and execute the command from top to bottom line, for me this is easy to understand. please correct me if the wrong

okay next, same when we do commit and push the project on git, add git add output_file_name.sql and git commit -m “message”. but impossible to write a message for commit by the same message with last commit, right?

in this case, we must make a dynamic value for the message commit. we can use timestamp or DateTime and put it on the variable for message commit.

in the command Linux, there is a function date to show date, we can use this to commit message

next, add this command to the script

look at the image below, I have put the command date in the variable message, in a bash script to create a variable is so simple, just create name_variable=” value ”. But in this case value of the message variable is returned from the function date, so we must wrap it in $(date).

next, add git commit -m “$message” and git push origin main

finally, we completed the write command in a bash script, save this file, and now you can run this file by cron job to automatically run the script.

open crontab -e and in my case I want to run this script at 12 am every day

after you run the command in this picture, you will see like this

and then add this command in the last of line

0 0 * * cd /home/user/Documents/Project/ && ./auto-git.sh

save with pres ctrl+x and then press y and enter.

everything is actually done, this cron job will execute auto-git.sh every 12am

please correct if there is a mistake, and thanks to my friend Adib and Fatkur because they are suggesting using script bash to solve my problem.

I hope you can understand what I’m saying and can take a few lessons from this article

--

--