Skip to content

Instantly share code, notes, and snippets.

@sutlxwhx
Last active September 1, 2021 15:23
Show Gist options
  • Save sutlxwhx/717efdfadd8052d456c2e4da16b0163b to your computer and use it in GitHub Desktop.
Save sutlxwhx/717efdfadd8052d456c2e4da16b0163b to your computer and use it in GitHub Desktop.
How to backup your LAMP / LEMP installation the right way

Introduction

This is the LAMP / LEMP environment backup guide in case you want or need to try my highload LEMP installation.

Basic usage

As a first thing we will setup a variable that will store current date and time. We will use year-month-day_hours-minutes-seconds format:

now=$(date +"%Y-%m-%d_%H-%M-%S")

Then we need to create a folder where Nginx configuration files will be stored:

mkdir /backup/$now/nginx/

We will repeat previous step for your PHP installation:

mkdir /backup/$now/php/

If you already have MySQL or MariaDB installed do this step too:

mkdir /backup/$now/mysql/

This is the main backup process, just copy files from one folder to another.
For Nginx:

cp -r /etc/nginx/ /backup/$now/nginx/

For PHP:

cp -r /etc/php/ /backup/$now/php/

For MySQL / MariaDB:

cp -r /etc/mysql/ /backup/$now/mysql/

Advanced Usage

Everything above can be also used in a bash script for any package you want. Just create backup.sh with the following text:

#!/usr/bin/env bash
now=$(date +"%Y-%m-%d_%H-%M-%S")
mkdir /backup/$now/nginx/
mkdir /backup/$now/php/
mkdir /backup/$now/php/
mkdir /backup/$now/mysql/
cp -r /etc/nginx/ /backup/$now/nginx/
cp -r /etc/php/ /backup/$now/php/
cp -r /etc/mysql/ /backup/$now/mysql/

Make it executable:

chmod +x backup.sh

And run from the current directory:

./backup.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment