Data is all around us. Even if we are unaware of it, data is the foundation of all we do, plan, and decide. IT systems undoubtedly rely on data, but we, as the data's creators, don't give it much thought, do we?
Data is more than just the information we have on file about, say, our customers or our products. The technology itself is also data. Let's use source code as an instance. If our code malfunctions for whatever reason, we won't be able to do anything with the data.
The significance of maintaining a database backup has been a hot issue in recent years. Yes, the data that your application displays to users is stored in the database, but what about the way that it is shown to users? You also need the code or software that executes your web application because a database by itself isn't very useful.
Although it is just as important for your organization as your databases, backing up your code is still less of a concern. It shouldn't be, though, and in this post, we'll show you how to back up code that's kept on GitHub with the use of a straightforward backup script.
To there are serious hazards, ranging from infrastructure issues and ransomware attacks to common human mistakes. Any kind of data loss is unaffordable. Can we say the same regarding source code backup, even if database backup is a common practice?
Why back up GitHub data?
Typically, you begin a new project on your local machine. When you're finished, you often publish the code to an online repository to keep it and make it easier to collaborate with other programmers or take it into production. GitHub is the most well-known online repository platform.
Even while Microsoft, a major player in technology, supports GitHub, it can occasionally experience downtime, which can be expensive for some companies. Additionally, it's possible that code on GitHub might be removed by accident by a user or by a repository administrator at a company.
While deleted GitHub repositories can be recovered, this must be done within 90 days after deletion and the affected code cannot have any forks. It can also be a laborious, tedious procedure. Without a backup, you risk losing a significant amount of time like this or, worse, a piece of important software.
Writing a GitHub backup script
You may avoid starting from scratch totally by using one of the many open-source packages that include a script to assist with backing up your work from GitHub.
This is an incredibly useful tool for developers who want to ensure that their work is safe and secure. With this backup system, developers can rest assured that their code, projects, and other important information will be stored in a secure location.
You may easily back up a GitHub repository using this program, which is a free source and distributed under the MIT License. This program is incredibly useful for developers who want to ensure their work is securely backed up and stored. It allows users to quickly and easily create a backup of their repository, which can be accessed from any device, at any time.
What your backup script needs
You must first decide what you want to back up and where you want the backed-up information kept before you can begin. Use the command below in your terminal to create a new folder called 'github-backup-script':
Move inside the folder:
Create two files, one for the script and the other for the config variables:
'backup.py' should be opened in a text editor. The code, which has a number of functions, should be copied and pasted into the document.
However, substitute the following code for the 'check name' function:
This fixes a problem that was brought up with this package.
You will now run a brief example and go over what each function in the 'backup.py' file performs.
You must have Python 3.6 or later installed in order to use this backup script, which was created in the Python programming language. It will be simpler for you to follow this post and comprehend the code if you have some familiarity with the Python programming language.
Run the command shown below in your terminal to install the third-party package 'requests' which is also required:
Some users might discover that executing 'pip' results in a "command not found" error because of how Python was installed on their machine. Take a look at these solutions to fix the problem.
There are four functions and the main function in the 'backup.py' file. When necessary, the main function can invoke additional functions.
The first function, 'get json()' will return a JSON object containing the information we obtained from the GitHub API.
The second function, 'check name()' determines whether the repository name is correct.
'mkdir()' the third function, creates a new directory for the repository on your local system.
Running the backup script
Inside the `config.json` file, add the following code:
To prove that you are a legitimate GitHub user, you must create a personal access token. You must choose the token's scope before it can be generated. Only click the Repo option before creating the token.
Paste the newly created token into the 'config.json' file as the token value.
The folder where the backups should be kept is indicated by the directory value. The owner value is optional, but supplying it signals that you want to exclude repositories connected to organizations you are a member of and only want the repository that is inside your account to be backed up.
Use the command listed below to run the script:
The config file is the parameter given to it. All of your GitHub account's repositories will be successfully backed up in this way.
When you run the script, all of the references and objects from the remote repository as well as any fresh branches are downloaded to your local repository. The output you receive is often located in the original repository's '.git' folder. The majority of the data that git manipulates and saves is located in this folder called '.git.' Use the command below to obtain a working directory for a certain repository:
Change 'the-github-repo-name' to the name of the repository and 'the-working-directory-name' to the name of the directory where the code should be copied. All of the files and directories will be extracted to the working directory as a result.
Limitations and alternatives
There are several restrictions when writing your own backup script or using an open source package and customizing it like we did before.
These solutions need a lot of overhead since you have to examine the code to see whether it is the best option for you. Since you'll be constrained to programming languages you're familiar with, this is another restriction in and of itself. Additionally, the majority of solutions don't have a lot of features and may only be able to partially recover information, such as bugs and pull requests.
There are additional challenges associated with this strategy. When using a script to backup, you must control the location of the backup data storage, both in terms of having the storage space available and assuring the security of the storage. You also need to keep an eye out for any API changes that can compromise your ability to connect to GitHub and maintain the codebase on top of everything else.
For a casual one-person project or a very small team, using a backup script can be a decent choice. As your project expands, it's important to think about a SaaS provider like Ottomatik that gives you the option to back up your data with no ongoing maintenance, a ton of useful features, and a simple user interface.
Your backups may be automated, and restoring them simply requires a single click, saving you the time of fiddling with scripts. Additionally, Ottomatik has the ability to back up and restore all information related to your repository, including commits, comments, problems, and other important data.
Why should you pick a backup tool instead of the script?
We may continue building new scripts and extending current ones to address these issues. But we may also employ currently available tools from other parties. Although it costs money, doing this helps us get beyond many challenges. Particularly in the early phases of a project or organization, writing your own Git backup scripts could appear more affordable, quicker, and superior. The primary benefit of this strategy is unquestionably personalization. Since it is our program, we are free to act anyway we like. It is entirely under our control. But the benefits of this method stop there.
Some of the key drawbacks of this strategy include the high (and steadily rising) future maintenance costs or the time needed to monitor and administer such scripts. It's important to remember that the dependability of the backup is not something we can guarantee.
Your codebase can become damaged in the same way that a database does. You've read in this post the significance of backing up your code. You've also examined an open-source backup script, including the functions' intended uses, and made a minor adjustment. This adjustment was made to ensure that the script would be more efficient and effective in its purpose. In addition, you have taken the time to thoroughly review the code, making sure that all of the functions are properly written and that there are no errors.
With Ottomatik Backups for GitHub, You can save precious time writing scripts manually. It offers a simple, one-click replacement for time-consuming and prone-to-error error manual backups. It automatically updates your backups, freeing up your developers to work on adding value for your clients. Your database has already been backed up; now, secure your company by doing the same with all of your other data.