Introduction

This article demonstrates backing up an existing Laravel site to GitHub. The next article demonstrates adding a GitHub WebHook to automatically update a Laravel site when you push updates to GitHub.

I have many websites in production status. I use different frameworks such as ASP.NET, Laravel, and Flask. I deploy using different frameworks to keep up to date with these frameworks. Recently, one of my Laravel sites went down so I decided to update that site, add source code control with Git, push to GitHub, and set up updates with a GitHub Webhook. This site has existed for more than 10 years and needs some attention. Since the site is small and rarely updated, I have been updating it in place. That is a bad development style. That is also a terrible style for testing, backups and site reliability. Time to fix that.

There are a variety of tools to use. I prefer command-line tools. For GitHub, I use the CLI tools GH and Git. I also use Visual Studio, which has excellent integration with GitHub, but I cannot run Visual Studio on my Ubuntu Azure virtual machine. Once I have the site stored in GitHub, I can use Visual Studio Enterprise for my day-to-day development.

Preparation

  1. Create an account on GitHub, if you do not already have one. [link]
  2. Download and install the GitHub CLI. [link]
  3. Download and install the Git CLI. [link]
  4. Go to the root of the Laravel site. For example: /var/www/example.com.
  5. Complete a file-based backup of your site using your standard tools. Use zip, tar, or cp -R to archive your files just in case of a mistake.
  6. Review your site’s files and directories. Determine items that should not be stored on GitHub such as .env, vendor, and node_modules. Make note of configuration files and secrets that must be manually set up on new deployments.

Suggestions

I organize my sites into a directory tree. Example for Laravel:

/var/www/example.com

  • apache2 – this directory stores the apache sites-available configuration file. The site configuration file is linked to /etc/apache2/sites-available/example.com.conf.
  • laravel – this directory contains a standard laravel distribution modified for my site’s content.
  • tools – this directory stores scrips and other files that I use for testing. Examples are scripts that post data to forms and APIs.
  • documents – this directory stores documents about the site design, database schema, setup, and installation, etc.

Note: Apache2 serves the Laravel site from laravel/public and not from laravel.

Install GH on Ubuntu:

This step installs the GitHub CLI. I use this tool to create and manage GitHub and GitHub Gist repositories.

curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo gpg --dearmor -o /usr/share/keyrings/githubcli-archive-keyring.gpg

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null

sudo apt update
sudo apt install gh

Additional installation instructions for other operating systems: [link]

Create a Git ignore file

This file informs the Git command-line tool which files and directories should not be stored in the Git repository. Examples of files and object types:

  • Anything with secrets. This includes passwords, encryption keys, credentials, etc.
  • File packages that should be installed or updated on the system and storing them in GitHub are redundant. Examples are the composer vendor directory and node_modules.
  • Any files that if leaked would create a problem.

Create a .gitignore file at the root of the site. Add the files and directories that should be ignored.

Example .gitignore that I use:

Initialze Git

Go to the root of the Laravel site. For my system, it is /var/www/example.com

Execute the command:

git init .

Initialized empty Git repository in /var/www/example.com/.git/

Setup author information

Setup Git with your name and email address. This information will be used for pushes to GitHub.

git config --global user.name "Your Name"
git config --global user.email you@example.com

Authenticate with GitHub

This step depends on how your system is set up. I use GitHub tokens to authorize the GitHub CLI.

gh auth login --with-token < mytoken.txt

Create a private GitHub repository

Execute the command:

gh repo create example_com --private

? Would you like to add a .gitignore? No
? Would you like to add a license? No
? This will add an "origin" git remote to your local repository. Continue? Yes
✓ Created repository mygithub/example_com on GitHub
✓ Added remote https://github.com/mygithub/example_com.git

Add files to the Git index

This step updates the Git index with current content. This index is stored on the system and not yet on GitHub. The index is a set of files stored in directory .git. This index is often called the staging area.

git add --all

Review files added to Git index

Before committing to GitHub for the first time, review the files added to the Git index.

git status

Remove a file from the index:

git rm --cached filename

Add a file to the index:

git add filename

Commit files to the local repository

This step will transfer the files from the index to the local repository.

git commit -m "Initial project commit"

Now if you run a status check, you will see that there are no files to commit. You have added the files to the local repository.

git status

On branch master
nothing to commit, working tree clean

Up to this point, we created an empty GitHub repository and an empty local repository. Then we added the project files to the Git Index. Then we committed those files to the local repository.

Now we will push the local repository to GitHub. Once this step completes, we will have a backup of the Laravel project in GitHub. Everything except for our secrets and unnecessary project files such as the laravel/vendor directory.

Push local repository to GitHub

git push -u origin master

Enumerating objects: 229, done.
Counting objects: 100% (229/229), done.
Delta compression using up to 2 threads
Compressing objects: 100% (211/211), done.
Writing objects: 100% (229/229), 734.83 KiB | 4.48 MiB/s, done.
Total 229 (delta 30), reused 0 (delta 0)
remote: Resolving deltas: 100% (30/30), done.
To https://github.com/mygithub/example_com.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

More Information

Photography Credits

I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Pixabay at Pexels.