How to set an automatic backup of your source code repository?
One day my colleague David was working on a task to migrate a repository from one platform to another while he was doing that the server crashed, the platform went down and all the data, code, and documentation available on the repository was lost. Have you ever imagined yourself in David’s Situation, What if this happens to you? Wondering what solution you can use instead of manual efforts?
You’re not alone.
We faced the same problem and found one of the best automation solutions that will work on all the platforms.
Problem:
We want to protect our source code repositories at any cost. It contains all of our code, documentation, and work history.
The real nightmare comes when you don’t have a backup. We all know that taking frequent backups manually is a tedious process.
So how can we do that through automation?
Solution:
There are many solutions to that. We have used this solution because it is based on bash script and works on all the platforms like Azure DevOps, Gitlab, and Bitbucket.
We are copying the Azure DevOps repository to GitHub. But as we said we can use this solution on every platform.
- First, log in to your GitHub account and create a GitHub repository. (If you don’t have it already)
- You have to generate a Personal access token at GitHub.
Settings -> Developer Settings -> Personal Access tokens
Generate and copy the token value. (refer to the following screenshot)
3. Navigate to your Azure repository. And create a pipeline there.
4. Add the following bash script task to the pipeline.
What will this script do?
This script will override the branch code of GitHub. In simple words, if you want to copy code from the feature branch of Azure to GitHub then:
- In GitHub – If the feature branch is present, it will override the code.
- In GitHub – if the feature branch is not present, then the branch will be created.
bash: |
git push -- \
Prune //ghp_iYwH9mwvmWVAFCD@github.com/SpurQLabs/SpurQLabsProject \
+refs/remotes/origin/master:refs/heads/master +refs/tags/:refs/tags/
displayName: 'Copy to GitHub'
condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
Note:
If your branch (in Azure) is master then no need to change but if your branch is main then you should replace the “master” with “main”.
Be Careful:
If you already have a repository (With Code) in GitHub, then instead of main or master use the feature branch in Azure. And then raised PR to the main branch.
And if you want to update code from the main branch from Azure to the main branch from GitHub then no worries. You are good to go. (It will override main branch code, if not there then will create a new one.).
- You can trigger your pipeline according to your requirements.
- If you want to schedule it on a daily, weekly, or monthly basis then you can use the “schedule”&”cron” options in the pipeline.
- Below is the link for your reference to schedule the pipeline
Run your pipeline.
And Done and Dusted..!
#Azure-pipeline.yml for reference
#Azure-pipeline.yml for reference
trigger:
- DemoSync #your feature Branch, Run this pipeline from your feature branch.
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
- bash: |
git push --prune https://ghp_cbvnn@github.com/SpurQLabs/SpurQLabsProject \
+refs/remotes/origin/DemoSync:refs/heads/DemoSync +refs/tags/*:refs/tags/*
displayName: 'Copy to GitHub'
condition: eq(variables['Build.SourceBranch'], 'refs/heads/DemoSync')
Conclusion
This is how the source code is migrated from azure repositories to GitHub repositories automatically after every check-in/PR merge.
As mentioned above, you can schedule it daily, Weekly, or monthly.
By profession a DevOps Engineer having hands-on experience in GitHub, Azure DevOps, AWS, CICD, Docker, Kubernetis and Server monitoring tools like ELK stack and Grafana. On the side I like doing Performance Testing with Jmeter and Security Testing with BurpSuite and ZAP Proxy. I also happen to be a certified X-ray Expert.