Skip to content

Commit

Permalink
Feature/deploy button (#84)
Browse files Browse the repository at this point in the history
* MariaDB version for ARM

* Submodules flattened
  • Loading branch information
b23prodtm authored Mar 10, 2024
1 parent faa74e4 commit 3aafad9
Show file tree
Hide file tree
Showing 149 changed files with 38,766 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .env
22 changes: 0 additions & 22 deletions .gitmodules

This file was deleted.

20 changes: 10 additions & 10 deletions Dockerfile.template.sed
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
s/%%BALENA_MACHINE_NAME%%/intel-nuc/g
s/(Dockerfile\.)[^\.]*/\1x86_64/g
s/%%BALENA_ARCH%%/x86_64/g
s/(BALENA_ARCH[=:-]+)[^$ }]+/\1x86_64/g
s/%%BALENA_MACHINE_NAME%%/raspberrypi3-64/g
s/(Dockerfile\.)[^\.]*/\1aarch64/g
s/%%BALENA_ARCH%%/aarch64/g
s/(BALENA_ARCH[=:-]+)[^$ }]+/\1aarch64/g
s#(IMG_TAG[=:-]+)[^$ }]+#\1latest#g
s#%%IMG_TAG%%#latest#g
s#(PRIMARY_HUB[=:-]+)[^$ }]+#\1betothreeprod/apache-php7#g
s#%%PRIMARY_HUB%%#betothreeprod/apache-php7#g
s#(SECONDARY_HUB[=:-]+)[^$ }]+#\1lscr.io/linuxserver/mariadb#g
s#%%SECONDARY_HUB%%#lscr.io/linuxserver/mariadb#g
s#(BALENA_ARCH[=:-]+)[^$ }]+#\1x86_64#g
s#%%BALENA_ARCH%%#x86_64#g
s#(BALENA_MACHINE_NAME[=:-]+)[^$ }]+#\1intel-nuc#g
s#%%BALENA_MACHINE_NAME%%#intel-nuc#g
s#(SECONDARY_HUB[=:-]+)[^$ }]+#\1linuxserver/mariadb#g
s#%%SECONDARY_HUB%%#linuxserver/mariadb#g
s#(BALENA_ARCH[=:-]+)[^$ }]+#\1aarch64#g
s#%%BALENA_ARCH%%#aarch64#g
s#(BALENA_MACHINE_NAME[=:-]+)[^$ }]+#\1raspberrypi3-64#g
s#%%BALENA_MACHINE_NAME%%#raspberrypi3-64#g
1 change: 0 additions & 1 deletion app/Plugin/Backups
Submodule Backups deleted from e00f57
230 changes: 230 additions & 0 deletions app/Plugin/Backups/Console/Command/BackupShell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php
/**
* CakePHP Database Backup
*
* Backups structure and data from cake's database.
* Usage:
* $ cake Backups.backup
* To backup all tables structure and data from default
*
* TODO
* Settings to choose datasource, table and output directory
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright 2012, Maldicore Group Pvt Ltd
* @link https://github.com/Maldicore/Backups
* @package plugns.Backups
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('CakeSchema', 'Model');
App::uses('ConnectionManager', 'Model');
App::uses('Inflector', 'Utility');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
// App::uses('Sanitize', 'Utility');

class BackupShell extends Shell {

/**
* Contains arguments parsed from the command line.
*
* @var array
* @access public
*/
var $args;
public $path;
public $dataSourceName;

/**
* Override main() for help message hook
*
* @access public
*/
function main() {

$dataSourceName = 'default';

$path = APP_DIR . DS .'Backups' . DS;

$Folder = new Folder($path, true);

$fileSufix = date('Ymd\_His') . '.sql';
$file = $path . $fileSufix;
if (!is_writable($path)) {
trigger_error('The path "' . $path . '" isn\'t writable!', E_USER_ERROR);
}

$this->out("Backing up...\n");
$File = new File($file);

$db = ConnectionManager::getDataSource($dataSourceName);

$config = $db->config;
$this->connection = "default";

foreach ($db->listSources() as $table) {

$table = str_replace($config['prefix'], '', $table);
// $table = str_replace($config['prefix'], '', 'dinings');
$ModelName = Inflector::classify($table);
$Model = ClassRegistry::init($ModelName);
$DataSource = $Model->getDataSource();
$this->Schema = new CakeSchema(array('connection' => $this->connection));

$cakeSchema = $db->describe($table);
// $CakeSchema = new CakeSchema();
$this->Schema->tables = array($table => $cakeSchema);

$File->write("\n/* Drop statement for {$table} */\n");
$File->write("SET foreign_key_checks = 0;");
// $File->write($DataSource->dropSchema($this->Schema, $table) . "\n");
$File->write($DataSource->dropSchema($this->Schema, $table));
$File->write("SET foreign_key_checks = 1;\n");

$File->write("\n/* Backuping table schema {$table} */\n");

$File->write($DataSource->createSchema($this->Schema, $table) . "\n");

$File->write("\n/* Backuping table data {$table} */\n");


unset($valueInsert, $fieldInsert);

$rows = $Model->find('all', array('recursive' => -1));
$quantity = 0;

if (sizeOf($rows) > 0) {
$fields = array_keys($rows[0][$ModelName]);
$values = array_values($rows);
$count = count($fields);

for ($i = 0; $i < $count; $i++) {
$fieldInsert[] = $DataSource->name($fields[$i]);
}
$fieldsInsertComma = implode(', ', $fieldInsert);

foreach ($rows as $k => $row) {
unset($valueInsert);
for ($i = 0; $i < $count; $i++) {
$valueInsert[] = $DataSource->value(utf8_encode($row[$ModelName][$fields[$i]]), $Model->getColumnType($fields[$i]), false);
}

$query = array(
'table' => $DataSource->fullTableName($table),
'fields' => $fieldsInsertComma,
'values' => implode(', ', $valueInsert)
);
$File->write($DataSource->renderStatement('create', $query) . ";\n");
$quantity++;
}

}

$this->out('Model "' . $ModelName . '" (' . $quantity . ')');
}
$File->close();
$this->out("\nFile \"" . $file . "\" saved (" . filesize($file) . " bytes)\n");

if (class_exists('ZipArchive') && filesize($file) > 100) {
$this->out('Zipping...');
$zip = new ZipArchive();
$zip->open($file . '.zip', ZIPARCHIVE::CREATE);
$zip->addFile($file, $fileSufix);
$zip->close();
$this->out("Zip \"" . $file . ".zip\" Saved (" . filesize($file . '.zip') . " bytes)\n");
$this->out("Zipping Done!");
if (file_exists($file . '.zip') && filesize($file) > 10) {
unlink($file);
}
$this->out("Database Backup Successful.\n");
}
}

function restore(){
$dataSourceName = 'default';

$path = APP_DIR . DS .'Backups' . DS;

$tmpath = APP_DIR . DS . 'tmp';

$backupFolder = new Folder($path);

// Get the list of files
list($dirs, $files) = $backupFolder->read();

// Remove any un related files
foreach ($files as $i => $file) {
if (!preg_match( '/\.sql/', $file)) {
unset($files[$i]);
}
}

// Sort, explode the files to an array and list files
sort($files, SORT_NUMERIC);
foreach ($files as $i => $file) {
$fileParts = explode(".", $file);
$backup_date = strtotime(str_replace("_", "", $fileParts[0]));
$this->out("[".$i."]: ".date("F j, Y, g:i:s a", $backup_date));
}

App::import('Model', 'AppModel');

$model = new AppModel(false, false);

// Prompt for the file to restore to
$this->hr();
$u_response = $this->in('Type Backup File Number? [or press enter to skip]');

if ($u_response == "") {
$this->out('Exiting');
} else {
$zipfile = $path.$files[$u_response];
if(array_key_exists($u_response, $files)){
$this->out('Restoring file: '.$zipfile);
$fileParts = explode(".",$files[$u_response]);

if(isset($fileParts[2]) && $fileParts[2]=='zip'){
$this->out('Unzipping File');
if (class_exists('ZipArchive')) {
$zip = new ZipArchive;
if($zip->open($zipfile) === TRUE){
$zip->extractTo($tmpath);
$unzipped_file = $tmpath.DS.$zip->getNameIndex(0);
$zip->close();
$this->out('Successfully Unzipped');
} else {
$this->out('Unzip Failed');
$this->_stop();
}
} else {
$this->out('ZipArchive not found, cannot Unzip File!');
$this->_stop();
}
}

if (($sql_content = file_get_contents($filename = $unzipped_file)) !== false){
$this->out('Restoring Database');
$sql = explode("\n\n", $sql_content);
foreach ($sql as $key => $s) {
if(trim($s)){
$result = $model->query($s);
}
}
unlink($unzipped_file);
} else {
$this->out("Couldn't load contents of file {$unzipped_file}, aborting...");
unlink($unzipped_file);
$this->_stop();
}
} else {
$this->out("Invalid File Number");
$this->_stop();
}
}

}
}
?>
82 changes: 82 additions & 0 deletions app/Plugin/Backups/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
Backup and Restore Plugin for CakePHP
=====================================
Version 1.0 for cake 2.x

This backup and restore plugin enables developers to quickly and easily backup and restore both database schema and data based on backup dates.

As you develop your application, changes to the database need to be managed in a consistent and easy manner, especially in a team environment where different people bring in different changes and this can get extremely difficult at times. Backup and restore enables you to share and co-ordinate database changes as well as data in a manageable fashion distributed using git itself, hence significantly removing the complexity of handling these changes (no more phpmyadmin for database updates). We have used backup and restore Plugin in production environments with multiple backups and for database roll-outs for fast deployments, without any hiccups. However, in the event we have missed a scenario we cannot be held responsible for any data loss, use this at your own risk. Said that we would love to fix any bug found by you! :)

Installation
=============

* Unzip or clone this plugin into your app/Plugin/Backups folder or the shared plugins folder for your CakePHP installation.
* Add the plugin to your app/Config/bootstrap.php using CakePlugin::load('Backups')

Usage
======

Backups
-------
* In your cakePHP application root folder (in a terminal) execute

<pre>
./app/Console/cake Backups.backup
</pre>

It will backup all the tables in the database and zip (if ZipArchive exits!)
the backup files are stored in ./app/Backups

![Backup](https://lh3.googleusercontent.com/-zPRwqzgjb7s/ULVo939aa3I/AAAAAAAACYQ/e85juiba1zg/s400/Selection_067.png)

Restoring
----------
* In your cakePHP application root folder (in a terminal) execute

<pre>
./app/Console/cake Backups.backup restore
</pre>

It will prompt you all available backup versions with date and time, just type a corresponding version (eg: 0) and hit enter

![Restore](https://lh5.googleusercontent.com/-O7FI-eLyiWc/ULVVtVhCmtI/AAAAAAAACXk/LNerKNlBQc8/s400/Selection_068.png)

Troubleshooting
----------------
* In case of any errors that is not predicted in the script you can always (manually) restore from one of the backup.

Notes
------
* The database user (in cakephp database config) must have rights to drop tables for restore to work! As the restore process drops the tables and restore them one by one.

Requirements
==============
PHP version: PHP 5.2+
CakePHP version: 2.1

Support
=========
For support and feature request, please create an issue:
https://github.com/Maldicore/Backups/issues

ToDo List
==========
* UnitTesting
* Settings to choose datasource and output directory

Contributing to this Plugin
=============================
Please feel free to contribute to the plugin with new issues, requests, unit tests and code fixes or new features. If you want to contribute some code, create a feature branch and send us your pull request.

License
Copyright 2012, Maldicore Group Pvt Ltd

Licensed under The MIT License: http://www.opensource.org/licenses/mit-license.php
Redistributions of files must retain the above copyright notice.

Copyright
==========
Copyright 2012
Maldicore Group Pvt Ltd
G. Reethimaage Aage
Male', Republic of Maldives
http://maldicore.com
1 change: 0 additions & 1 deletion app/Plugin/Datasources
Submodule Datasources deleted from b74192
Loading

0 comments on commit 3aafad9

Please sign in to comment.