Browse Source

init

master
Christoph Miksche 2 years ago
commit
bb99fbf2a4
5 changed files with 99 additions and 0 deletions
  1. +18
    -0
      README.md
  2. +76
    -0
      backup.php
  3. +3
    -0
      backup/index.php
  4. +1
    -0
      index.php
  5. +1
    -0
      myphp-backup

+ 18
- 0
README.md View File

@@ -0,0 +1,18 @@
# php-sharedhost-backup

Simple script to create a backup of your MySQL Databases and files on shared host
environments.

## General Information

- Uses https://github.com/daniloaz/myphp-backup for MySQL Database Backups (GPL-3.0
License)
- Uses https://gist.github.com/MarvinMenzerath/4185113 for zipping the root Webspace dir
- License: GNU General Public License 3.0
- Author: Christoph Daniel Miksche (m5e.de)

## Installation

- Upload the dir of this script to your webhost in the root directory
- Start the backup.php file via browser or cron
- The backups are saved in the "backup" directory of this script

+ 76
- 0
backup.php View File

@@ -0,0 +1,76 @@
<?php

/**
* Define database parameters here
*/
define("DB_USER", 'your_username');
define("DB_PASSWORD", 'your_password');
define("DB_NAME", 'your_db_name');
define("DB_HOST", 'localhost');
define("BACKUP_DIR", 'backup');
define("TABLES", '*'); // Full backup
//define("TABLES", 'table1 table2 table3'); // Partial backup
define("CHARSET", 'utf8');
define("GZIP_BACKUP_FILE", true); // Set to false if you want plain SQL backup files (not gzipped)

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');

// Report all errors
error_reporting(E_ALL);
// Set script max execution time
set_time_limit(900); // 15 minutes

// Database Backup Skript from https://github.com/daniloaz/myphp-backup
require_once('myphp-backup/myphp-backup.php');

// Function to Recursively Backup Files & Folders to ZIP-File
/*
* PHP: Recursively Backup Files & Folders to ZIP-File
* MIT-License - 2012-2018 Marvin Menzerath
*/
function zipData($source, $destination) {
if (extension_loaded('zip')) {
if (file_exists($source)) {
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
$source = realpath($source);
if (is_dir($source)) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = realpath($file);
if (is_dir($file)) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file)) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
} else if (is_file($source)) {
$zip->addFromString(basename($source), file_get_contents($source));
}
}
return $zip->close();
}
}
return false;
}

/**
* Instantiate Backup_Database and perform backup
*/

if (php_sapi_name() != "cli") {
echo '<div style="font-family: monospace;">';
}

$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$result = $backupDatabase->backupTables(TABLES, BACKUP_DIR) ? 'OK' : 'KO';
$backupDatabase->obfPrint('Backup result: ' . $result, 1);

if (php_sapi_name() != "cli") {
echo '</div>';
}

// Start the file backup!
zipData('../', BACKUP_DIR.'/'.date('Y-m-d').'-files.zip');

+ 3
- 0
backup/index.php View File

@@ -0,0 +1,3 @@
<?php



+ 1
- 0
index.php View File

@@ -0,0 +1 @@
<?php

+ 1
- 0
myphp-backup

@@ -0,0 +1 @@
Subproject commit 4485ced6a507c741c19796d4bf15e7c418177a82

Loading…
Cancel
Save