Periodic Database Backup to Gitlab Private Repository Using Flysystem and Cronjob

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • Cloudways - Managed Cloud Hosting Platform
  • Scout APM - Less time debugging, more time building
  • SonarLint - Clean code begins in your IDE with SonarLint
  • flysystem-gitlab-storage

    Gitlab Storage filesystem for Flysystem (https://flysystem.thephpleague.com/docs/).

    Flysystem Gitlab Adapter by Roy Voetman

  • mysqldump-php

    PHP version of mysqldump cli that comes with MySQL

    Mysqldump PHP by ifsnop

  • Cloudways

    Managed Cloud Hosting Platform. Cloudways leverages advanced technology and powerful servers. Cloudways is a one-click managed cloud hosting platform that provides cloud application and server management solutions.

  • profile

    Visit Gitlab Access Token page and create your access token:

  • ntfy

    Send push notifications to your phone or desktop using PUT/POST (by binwiederhier)

    $db_host = 'YOUR_DB_HOST'; $db_user = 'YOUR_DB_USER'; $db_pass = 'YOUR_DB_PASS'; $db_name = 'YOUR_DB_NAME'; // Create database name $database_name = 'backup-on-YOUR-DB-NAME-'.date('d-m-Y').'.sql'; // Define path for dump on disk $app_path = 'YOUT_PATH'; if ( file_exists($app_path . $database_name) ) { $database_name = date('His').'-'.$database_name; // Dump database $dump = new IMysqldump\Mysqldump('mysql:host='. $db_host .';dbname='. $db_name .'', $db_user, $db_pass); $dump->start( $app_path . $database_name ); } else { // Dump database $dump = new IMysqldump\Mysqldump('mysql:host='. $db_host .';dbname='. $db_name .'', $db_user, $db_pass); $dump->start( $app_path . $database_name ); } $file_content = file_get_contents( $app_path . $database_name ); $dir_backup = 'db-'.strtolower(date('F-Y')); // Result like: db-december-2021 // Gitlab Flysystem Config $personal_access_token = "GITLAB_ACCESS_TOKEN"; $project_id = "PROJECT_ID"; $branch = "BRANCH_NAME"; $cloud_url = "https://gitlab.com"; // <-- default or use your gitlab instance url // Create a GitLab Client to talk with the API $client = new Client( $personal_access_token , $project_id, $branch, $cloud_url); // Create the Adapter that implements Flysystems AdapterInterface $adapter = new GitlabAdapter($client); // Use FileSystem $filesystem = new Filesystem($adapter); // Check if directory backup is exists on repository if ( $filesystem->has($dir_backup) ) { // If exists then write database inside $filesystem->write($dir_backup.'/'.$database_name, $file_content); } else { // If not exists then create directory $filesystem->createDir($dir_backup); // then write database inside $filesystem->write($dir_backup.'/'.$database_name, $file_content); } file_get_contents('https://ntfy.sh/', false, stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => "Content-Type: text/plain\r\n" . "Title: Daily Database Backup!\r\n" . "Priority: default\r\n" . "Tags: bell,tada,partying_face", 'content' => 'Backup successful 😀' ] ]));

  • Flysystem

    Abstraction for local and remote filesystems

    Ok, I need the Flysystem Library to do CRUD transactions with the Gitlab API.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts