Read Thought

How to use Github as a Disaster Recovery Backup System for Spatie Backups

As a Laravel Developer, you've been using Spatie Backups to generate backups. But what happens if that server goes down?Disaster Recovery Backup systems for your app / company seems like a complex solution. Especially if you don't have $$$. This is a simple solution where you can use something you know, Git & Github.


Right now, the biggest problem we have is, all of our

1. Initialize a Git Repository in Your Storage Folder:

If you haven't already, navigate to your storage folder and initialize a new Git repository:

cd storage/path-to-your-backup-folder
git init

2. Add the Remote Repository:

Link the local repository to your GitHub repository:

git remote add origin git@github.com:your-github-username/your-repository-name.git

3. Create the Laravel Console Command:

Generate a new console command:

php artisan make:command GithubBackup

4. Implement the Command:

Edit the GithubBackup.php file:

protected $signature = 'backup:github';
protected $description = 'Backup the project to a GitHub repository';

public function handle()
{
    // Navigate to your backup directory
    $backupDir = storage_path('path-to-your-backup-folder');

    // Git commands to add, commit, and push
    $commands = [
        "cd {$backupDir}",
        'git add .',
        'git commit -m "New backup"',
        'git push -u origin master'
    ];

    // Execute the commands
    foreach ($commands as $command) {
        $this->line(shell_exec($command));
    }

    $this->info('Backup successfully uploaded to GitHub!');
}

5. SSH Authentication:

Ensure that the server where you're running this command has been set up with the necessary SSH keys to push to your GitHub repository:

  1. If you haven't already, generate an SSH key pair on your server:
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
  1. Add the public key (~/.ssh/id_rsa.pub) to your GitHub account. You can do this in the "SSH and GPG keys" section of your GitHub settings.

  2. Ensure the private key (~/.ssh/id_rsa) is available and accessible when the Laravel command runs.

6. Schedule the Command:


published on 15 OCT 2023 by Leonard Selvaraja Fernando on leonardselvaraja.in