Developed with love by KnpLabs Hire us for your project!
108

CloudBackupBundle

by dizda

Be able to backup your database(s) and upload it to the cloud (Dropbox, CloudApp, GoogleDrive, etc.)

Build Status

CloudBackupBundle

This bundle helps you to backup your databases and upload it to the cloud with only one Symfony2 command.

You can :
* Dump one database
* Dump all databases
* Different types of databases can be dumped each time
* Upload to several Cloud services

Databases supported :
* MongoDB
* MySQL
* PostgreSQL (excluding all_databases option)

Cloud services supported :
* Dropbox (with the help of DropboxUploader by hakre)
* CloudApp (thanks to CloudAPP-API-PHP-wrapper)
* Amazon S3 (through KnpGaufretteBundle)
* Google Drive (thanks to HappyrGoogleSiteAuthenticatorBundle)

But also :
* Local (through KnpGaufretteBundle)
* FTP (through KnpGaufretteBundle)
* sFTP (through KnpGaufretteBundle)
* GridFS (through KnpGaufretteBundle)
* MogileFS (through KnpGaufretteBundle)

are supported :-)

Compressors supported :
* Tar - fast and medium effective, don't support password
* Zip - fast and medium effective, support password
* 7zip - very slow and very effective, support password

Splitters supported:

  • ZipSplit - split a zipfile into smaller zipfiles

Installation (>=Symfony 2.1)

Composer

Download CloudBackupBundle and its dependencies to the vendor directory. You can use Composer for the automated process:

$ php composer.phar require dizda/cloud-backup-bundle "~1.6"

Composer will install the bundle to vendor/dizda directory.

Adding bundle to your application kernel

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Knp\Bundle\GaufretteBundle\KnpGaufretteBundle(),
        new Dizda\CloudBackupBundle\DizdaCloudBackupBundle(),
        // ...
    );
}

Configuration

Here is the default configuration for the bundle:

dizda_cloud_backup:
    # By default backup files will have your servers hostname as prefix
    # such as: hostname_2014-01-01_21-08-39.tar
    output_file_prefix: hostname
    timeout: 300
    error_notification: 
        from: dizda@backupbundle.com
        to: your@email.com
    processor:
        type: tar # Required: tar|zip|7z
        options:
            compression_ratio: 6
            password: qwerty
            # Split into many files of `split_size` bytes
            split:
                enable: false # Default false
                split_size: 1000 # Make each zip files no larger than "split_size" in bytes
                storages: [ Dropbox, CloudApp, GoogleDrive, Gaufrette ] # Which cloud storages will upload split files
    folders: [ web/uploads , other/folder ]
    cloud_storages:
        # Dropbox account credentials (use parameters in config.yml and store real values in prameters.yml)
        dropbox:
            user:     ~  # Required
            password: ~  # Required
            remote_path: ~ # Not required, default "/", but you can use path like "/Accounts/backups/"
        # CloudApp account. Can be optional, like dropbox.
        cloudapp:
            user:        ~ # Required
            password:    ~ # Required
        # or you can use Gaufrette as well (optional)
        gaufrette:
            service_name: ~  # Gaufrette filesystem service name
        google_drive:
          token_name: ~ # Required
          remote_path: ~ # Not required, default "/", but you can use path like "/Accounts/backups/"

    databases:
        mongodb:
            all_databases: false # Only required when no database is set
            database:     ~ # Required if all_databases is false
            db_user:     ~ # Not required, leave empty if no auth is required
            db_password: ~ # Not required

        mysql:
            all_databases: false # Only required when no database is set
            database: ~          # Required if all_databases is false
            db_host: localhost   # This, and following is not required and if not specified, the bundle will take ORM configuration in parameters.yml
            db_port: ~           # Default 3306
            db_user: ~
            db_password: ~

        postgresql:
            database: dbname     # Required
            db_host: localhost   # This, and following is not required and if not specified, the bundle will take ORM configuration in parameters.yml
            db_port: ~           # Default 5432
            db_user: ~
            db_password: ~

It is recommended to keep real values for logins and passwords in your parameters.yml file, e.g.:

# app/config/config.yml
dizda_cloud_backup:
    processor:
        type: tar
        options:
            password: %dizda_cloud_archive_password%

    cloud_storages:
        dropbox:
            user:        %dizda_cloud_dropbox_user%
            password:    %dizda_cloud_dropbox_password%
            remote_path: %dizda_cloud_dropbox_remote_path%

    databases:
        mongodb:
            all_databases: false
            database: %dizda_cloud_mongodb_user%
            db_user:  %dizda_cloud_mongodb_user%
            db_pass:  %dizda_cloud_mongodb_password%

        mysql:
            # When no parameters is specified under mysql, the bundle taking those from parameters.yml

        postgresql:
            # When no parameters is specified under postgresql, the bundle taking those from parameters.yml
# app/config/parameters.yml
    # ...
    database_driver: pdo_mysql
    database_host: localhost
    database_port: null
    database_name: myDatabase
    database_user: myLogin
    database_password: myDatabasePassword
    # ...
    dizda_cloud_dropbox_user:     myDropboxUser
    dizda_cloud_dropbox_password: MyDropboxPassword
    dizda_cloud_mongodb_user:     mongodbUser
    dizda_cloud_mongodb_password: mongodbPass
    dizda_cloud_archive_password: ArchivePassword
    # ...

Usage

The bundle adds one command to symfony console: app/console dizda:backup:start which you execute periodically as a cron job.
For example the following cron command dumps your database every days at 6am on a server :
```

m h dom mon dow command

0 6 * * * php /opt/www/symfony-project/app/console dizda:backup:start
```

Info : To edit crontab for the user www-data (to prevent permissions error) :
bash
$ crontab -u www-data -e

or simply

$ php app/console dizda:backup:start

You may point concrete archiver in command line:

$ php app/console dizda:backup:start zip

In addition, using -F or --folder option the folders also will be added to the backup.

If some problems occurs during the backup process and error_notification.to parameter is set the bundle will send an email to that address.

Which archiver do I use?

tar and zip archivers are produce the same size of compressed file, but tar compresses faster.
7z archiver is very slow, but has double effectiveness.
tar archiver do not support encryption, other archivers support.

Note Your system may not have the zip and 7z archivers installed. But tar is installed in common case.

Guide to choice:
* If you don't need password protection and you have enough disk space, the best choice is tar.
* If you need password protection and you have enough disk space, the best choice is zip.
* If you haven't enough disk space (or you will do backup often) and you backup only text data (e.g. database dumps), the best choice is 7z.

Note Any archiver good compress text files (and better compress structured texts e.g. sql, css, html/xml).
But binary files (images, audio, video) will not be well compressed. If you have small database dump and big binary data, the best choice will be tar or zip.

Comparison of archivers

Uncompressed archive contents sql dump of 42.2M size. This table represents effectiveness of archivers.
Third column contents compressed archive file and percent of compression (low is better).
Fourth column contents compression time and its ratio (to first line) (low is better).

archiver compression archive size execution time
default (6)
best (9)
default (6)
best (9)
default (5)
best (9)

Capifony integration

If you are using capifony for deployment you can grab the sample task for easier backups.

Add the following task in your deploy.rb file
ruby
namespace :symfony do
namespace :dizda do
namespace :backup do
desc "Upload a backup of your database to cloud service's"
task :start do
run "#{try_sudo} sh -c 'cd #{current_release} && #{php_bin} #{symfony_console} dizda:backup:start #{console_options}'"
end
end
end
end

This adds symfony:dizda:backup:start command to capifony. To launch it automatically on deploy you might use:

# 1) Launches backup right before deploy
before "deploy", "symfony:dizda:backup:start"

# 2) Launches backup after deploy
after "deploy", "symfony:dizda:backup:start"

End

This bundle was inspired from KachkaevDropboxBackupBundle.

It is Symfony2.1, 2.2 and 2.3+ compatible.

Enjoy, PR are welcome !

Copyright (c) 2013 Jonathan Dizdarevic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
dizda_cloud_backup:
output_file_prefix: ks28273.kimsufi.com
folders: []
cloud_storages:

# Dropbox account credentials (use parameters in config.yml and store real values in prameters.yml)
dropbox:
user: ~ # Required
password: ~ # Required
remote_path: /

# CloudApp
cloudapp:
user: ~ # Required
password: ~ # Required

# Any gaufrette adapter is supported
gaufrette:
service_name: ~ # Required
databases:
mongodb:
all_databases: true
database: false
db_host: localhost
db_port: 27017
db_user: ~
db_password: ~
mysql:
all_databases: true
database: ~
db_host: ~
db_port: 3306
db_user: ~
db_password: ~
postgresql:
database: ~
db_host: localhost
db_port: 5432
db_user: ~
db_password: ~
  • Merge pull request #49 from dafuer/master
    By dizda, 1 year ago
  • Cleaning code following dizda's advices
    By David Fuertes, 1 year ago
  • Merge branch 'master' of https://github.com/dafuer/CloudBackupBundle
    By David Fuertes, 1 year ago
  • Multiple email can be set for email notification
    By David Fuertes, 1 year ago
  • Cleaning code for error notification feature
    By David Fuertes, 1 year ago
  • Improved email notification feature following @dizda's advices
    By David Fuertes, 1 year ago
  • Added new functionality: Email notification if there was error
    By David Fuertes, 1 year ago
  • Multiple email can be set for email notification
    By David Fuertes, 1 year ago
  • Cleaning code for error notification feature
    By David Fuertes, 1 year ago
  • Improved email notification feature following @dizda's advices
    By David Fuertes, 1 year ago
  • fix readme
    By dizda, 1 year ago
  • Merge pull request #43 from crash21/ZipSplit
    By dizda, 1 year ago
  • Changed split size configuration and removed it from initialize method
    By crash21, 1 year ago
  • After rebase fixed configuration variables
    By crash21, 1 year ago
  • Added new functionality: Email notification if there was error
    By David Fuertes, 1 year ago
  • Fixed configuration
    By crash21, 1 year ago
  • Fixed conflicts
    By crash21, 1 year ago
  • Merge branch 'ZipSplit' of https://github.com/crash21/CloudBackupBundle into ZipSplit
    By crash21, 1 year ago
  • Fixed variables to be camel case
    By crash21, 1 year ago
  • Fixed
    By crash21, 1 year ago
  • Added some methods in BaseSplitter.php
    By crash21, 1 year ago
  • Removed redundant code
    By crash21, 1 year ago
  • Minor README.md fix
    By crash21, 1 year ago
  • Added BaseSplitter.php abstract class for all slitters
    By crash21, 1 year ago
  • Added ZipSplit Option
    By crash21, 1 year ago
  • Fixed variables to be camel case
    By crash21, 1 year ago
  • Fixed
    By crash21, 1 year ago
  • Merge pull request #46 from Smart-Core/master
    By dizda, 1 year ago
  • Merge pull request #47 from jongotlin/dropbox-composer
    By dizda, 1 year ago
  • DropboxUploader dependency moved to composer
    By jongotlin, 1 year ago