Git Linux Security Ubuntu

Daily Git auto commits using cron job

Daily Git auto commits using cron job
Written by Yogesh Koli

Got question on how to set CRONJOB to auto git commits changes on daily basis? you might probably want to implement this for your WordPress project right or may be for some different projects or code, eventually the point is you want to secure your changes on git so that if someone hacked your server and did some updates like deleting the files or modifying the files you don’t have to worry about if you loss your server backup the git commits will always be there for you to reset the project files from appropriate commit.

Hacking could not be the only reason there are a lot of points and keeping backup of updated changes will be always better.

So just be with me, In this tutorial I will give you details along with script to achieve this task on ubuntu server.

1: Configure SSH Keys:

Note – if you have already configured your server ssh keys to your development platform ex. Github. then you can jump on to next step you don’t have to do it again. if not go ahead and follow next instructions.

SSH to your server using terminal where you have your project up and running, make sure you have sudoers access.

Get into root account:

Login to Server

Login to Server

Now we will have to generate new SSH key and configure with git provider it can be Github, Gitlab or Bitbucket etc.

Press enter to generate the key and use this command to print the newly generated key on terminal like showing in this screen:

Generate New SSH key on Ubuntu Server

Generate New SSH key on Ubuntu Server

Now copy the key you have generate and login to your repository and go to the profile settings and ssh key section, in my case I am using bitbucket it can be different for you if you are one some other different platform but steps should be probably same.

i) Login to your account:

Login to bitbucket

Login to bitbucket

ii) Add new key:

Add SSH Key

Add SSH Key

2: Git Commits Script:

Now will have to write commits script into the executable file, before going to do that make sure you have a complete path of your project from server.

create new file called commits.sh and open into edit mode and paste following script make sure to update project path according to yours.

Save this file and close and note down the location I mean the full path of the this file where you have stored, in my case I have it on /root/commits.sh location.

Now we need to make sure the file is executable so let’s give execute permission to commits.sh file using following command:



3: Update Crontab:

Now here it comes into cron job, we needs to set up new job to execute on daily basis.

Use following command to edit crontab file:

Add this line at the end of the crontab file:

Save and close the file and we are done, our new script will execute daily at 6:30 AM and all modifications from the project will be sent to the git without any issues.

Let me know if you have any questions.

If you find this tutorial helpful, make sure to like and share with other sharing is caring, share knowledge, also don’t forget to comment in the comment box below.

Next Recommended Learning – Automate Database Backup with Email Notifications using PHP CRON Job

About the author

Yogesh Koli

Yogesh Koli is a software engineer & a Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 5+ years of experience working with the front-end, back-end, web application development, and system design.

Leave a Comment