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.

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:

Setup author information

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

Authenticate with GitHub

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

Create a private GitHub repository

Execute the command:

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.

Review files added to Git index

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

Remove a file from the index:

Add a file to the index:

Commit files to the local repository

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

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.

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

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.