“Git”ty Up and Kick the Bucket
By Rimpi Mathur, Technical Architect
If you’re managing a Linux server and need to sync with a cloud-based Atlassian Bitbucket Git repository, you’re in the right place. This guide walks you through two straightforward methods to establish a secure connection between your server and Bitbucket: creating new SSH keys or using ones that already exist. Whether you’re setting up a fresh connection or simply plugging into an existing Git setup, this article covers all the steps—from generating keys and configuring SSH to verifying repository links and setting up automated pulls with a cron job. Let’s get your server “git”ting in sync with Bitbucket!
Option 1: Setting up new keys and ssh connection:
- Create your ssh key in /{username}/.ssh, using the command: ssh-keygen -t rsa -b 2048
This will create two files: “id_rsa.pub” and “id_rsa”
2. Copy the content of the .pub file to the Bitbucket site. To copy the key execute “cat id_rsa.pub” in the terminal, this will show you the content of the key.
3. Now, on the Atlassian Bitbucket website, navigate to your Personal Bitbucket settings.
Click on SSH keys and “Add key”
4. Go back to the terminal and check if you can SSH to Bitbucket
5. Go to your repository folder in your terminal and execute the following
git remote set-url origin <remote_url>
6. After the remote is successfully changed, execute the following command to confirm if the location is correct.
git remote -v
Option 2: Using existing ssh keys and Bitbucket locations:
1. Look for the .ssh folder. This is normally under your default home directory. You can use this command to locate it: sudo find / -name .ssh
2. Open the “id_rsa.pub” file and copy the contents.
Step 3 for adding the key to Bitbucket is the same as above option 1.
4. Come back to the terminal and test if you can connect. ssh -T git@bitbucket.org
5. On the terminal search for the hidden folder .git Sudo find / -name .git
6. .git folder has multiple files. Open the “config” file and replace the URL with the git repository URL
7. Check if the new URL shows up using the command git remote -v
Now that git is talking to Bitbucket, let’s write a shell script to pull the content from the repository, which can be run on demand or can be set up as a cronjob to run daily at 5:00 am.
Let’s name the script as mydata.sh
Set up the cronjob using the below:
00 5 * * 0-6 /<path of shell script>/mydata.sh
About the Author
As a certified AWS SysOps Administrator, Rimpi Mathur has over twenty-four years of experience with AWS and Oracle portal and web application support and development which includes integration with multiple backend systems. Her experience includes requirements gathering, technical design, development, installing/configuring environments and deploying applications. Rimpi has a working knowledge of AWS, Ansible, AJAX, CSS, RDS, Hibernate, HTML, Java, REST, JavaScript, Spring, SQL, PostgreSQL, Aurora Global Database, Terraform, CloudFormation, SonarQube, Docker, WebLogic Portal, and Web Services. Previous clients worked with include Clearnova, Westfield Insurance and Satyam Computers.