Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Backing up with Laravel (laracon)

Freek Van der Herten
August 24, 2016
490

Backing up with Laravel (laracon)

Freek Van der Herten

August 24, 2016
Tweet

Transcript

  1. Backing up 

    with 

    Laravel

    View Slide

  2. About me
    Freek Van der Herten
    Partner & developer at SPATIE
    @freekmurze
    https://murze.be

    View Slide

  3. About me
    phpantwerp.be
    Dries Vints
    @driesvints
    Frederick Vanbrabant
    @TheEdonian

    View Slide

  4. Since 2003
    Websites, applications and webshops
    Team
    4 developers, 1 manager
    Specialization
    Laravel development

    View Slide

  5. OPEN SOURCE SOFTWARE

    View Slide

  6. Open source software
    We use a lot of it
    Nginx, Laravel, Ubuntu, Elasticsearch, Memcached, Redis,
    Beanstalkd, MySQL, Composer, Glide, Homestead, Flysystem,
    Fractal, Carbon, PHPUnit, NPM, React, jQuery, Java, SSH, Sequel
    Pro, BackupPC …
    Basically everything listed in composer.json and package.json

    View Slide

  7. ± 80 packages on Packagist
    + 800 000 downloads
    A lot of benefits: learning, forced to write
    documentation and tests, showing the quality of
    our work, eat our own dogfood
    We create a lot of it
    Open source software

    View Slide

  8. Laravel Permission
    Laravel Analytics
    Laravel Reponsecache
    Laravel Medialibrary
    Laravel Fractal
    Open source software

    View Slide

  9. Associate users with roles and permissions
    Laravel Permission
    $role = Role::create(['name' => 'writer']);

    $role->givePermissionTo('edit articles');

    $user->assignRole('writer');

    $user->can('edit articles’);

    View Slide

  10. Retrieve data from Google Analytics
    Laravel Analytics
    Analytics::fetchMostVisitedPages();
    Analytics::fetchVisitorsAndPageViews();

    Analytics::getTopReferrers();

    View Slide

  11. Speed up your application 

    by caching the entire response
    Laravel Responsecache
    RESPONSE CACHE
    REQUESTS
    APP
    1
    2

    View Slide

  12. Associate files with Eloquent Models
    Laravel Medialibrary
    $newsItem->addMedia($file)
    ->toCollection('images');

    $newsItem->getMedia('images')->first()->getUrl();

    $newsItem->getMedia('images')->first()->getUrl('thumb');


    $newsItem->addMedia($bigFile)
    ->toCollectionOnDisk('downloads', 's3');

    View Slide

  13. Transformation layer for complex output
    Fractal
    $manager = new Manager();

    $resource = new Collection($books, new BookTransformer());

    $manager->parseIncludes('characters');

    $manager->createData($resource)->toArray();

    View Slide

  14. Easy integration with Fractal in Laravel
    Laravel Fractal
    fractal()

    ->collection($books)

    ->transformWith(new BookTransformer())

    ->includeCharacters()

    ->toArray();

    View Slide

  15. https://spatie.be/opensource/laravel

    View Slide

  16. Not free!

    View Slide

  17. POSTCARDWARE

    View Slide

  18. Spatie

    Samberstraat 69D
    2060 Antwerp

    Belgium

    View Slide

  19. LET’S TALK BACKUPS

    View Slide

  20. There is no 

    one size fits all solution
    for backups

    View Slide

  21. Small sites
    Shared hosting
    Backups by hosting provider
    Did not do anything regarding backups ourselves
    Hosting
    Up until a few year ago

    View Slide

  22. Bigger applications
    Learned basic server management through
    resources like serversforhackers.com
    Digital Ocean
    Forge / Ansible
    Hosting
    How we are currently doing it

    View Slide

  23. HORROR STORIES

    View Slide

  24. Horror Story #1
    Earlier today, our Cloud Operations team was alerted to some
    performance issues affecting the physical server that hosts your droplet.
    … The damage was serious enough that this droplet was lost and not able
    to be restored.
    One fine morning I got this mail from Digital Ocean
    Full story: https://murze.be/2016/02/today-digitalocean-lost-our-entire-server/

    View Slide

  25. Horror Story #2
    This message is a follow-up to our previous emails. Despite the very best
    efforts of our engineering and datacenter operations teams, unfortunately
    the host machine that your cloud server resides on was not able to be
    recovered. At this point, there is complete data loss on the server.
    Horror can occur everywhere. A mail from Rackspace:
    Source: https://news.ycombinator.com/item?id=3982571

    View Slide

  26. Ok, I’ll just use

    the backup service 

    from my cloud provider

    View Slide

  27. Horror Story #3

    View Slide

  28. Horror Story #3

    View Slide

  29. Relying on DO backups 

    is not enough!
    Only weekly snapshots
    Potentially a lot of data could get lost
    All the backups in the same datacenter
    If Digital Ocean is down backups can’t be accessed

    View Slide

  30. SOLUTIONS

    View Slide

  31. DIY: A bash script
    Hosted service: https://ottomatik.io/
    Open source: BackupPC, Bacula
    Solutions
    Do not put your eggs in one basket

    View Slide

  32. https://serversforhackers.com/video/backup-to-s3 interface
    Dumps the database with mysqldump
    Uploads dump + files using was cli tool to S3
    Use cron to frequently run script
    Downside: no notifications when something goes wrong


    A bash script

    View Slide

  33. Backups as a service
    Not free
    Install Ottomatik Server Agent on every server
    Easy to use interface
    Bring your own storage
    Ottomatik.io

    View Slide

  34. Open source software
    Installed onto a control server
    SSH’s into servers to be backed up
    Uses hard links to save disk space
    Some system administration knowledge is required to setup up
    BackupPC SERVERS
    BackupPC
    SSH

    View Slide

  35. LARAVEL BACKUP

    View Slide

  36. Backup files and databases to one or more filesystems
    Cleanup old backups
    Sending notifications
    Easily installed into any Laravel application
    Laravel Backup

    View Slide

  37. View Slide

  38. Backup files & databases
    FILES
    DATABASE
    SERVER
    S3
    DROPBOX
    DB DUMP
    .ZIP
    .SQL

    View Slide

  39. Backups use a lot of storage
    Artisan task to clean up old backups
    Fully configurable
    Will never delete the youngest backup
    Grandfather-father-son rotation scheme
    Cleanup old backups

    View Slide

  40. Grandfather-father-son
    rotation scheme
    FEW TIMES
    A DAY
    DAILY (16)
    WEEKLY (8)
    MONTHLY (4)
    YEARLY(2)
    KEEP

    EVERYTHING
    KEEP DAILY BACKUPS
    KEEP WEEKLY BACKUPS
    KEEP MONTHLY BACKUPS
    KEEP YEARLY BACKUPS

    View Slide

  41. Detects when no backups were made in a certain amount of days
    Detects if there is too much storage used
    Fully configurable
    Monitor backups

    View Slide

  42. On (un)successful events (backup failed, youngest backup is too
    old, backup uses too much storage …)
    Mail, Slack, Telegram, Pushover
    Fully configurable
    Easy to extend
    Sending notifications

    View Slide

  43. DEMO

    View Slide

  44. Your Laravel app may break
    Server may be down
    Install the package in a Laravel app on a separate server
    Monitor the backups of all your apps from there
    Monitoring best practices

    View Slide

  45. Monitoring best practices
    APPS
    BACKUPS
    MONITOR

    View Slide

  46. Install a Laravel app on the same server
    Point the configuration the the database/directories you which to
    backup
    Profit!
    Backing up a non Laravel app

    View Slide

  47. Backup to multiple filesystems
    Get notified when something goes wrong
    Cleans up older backups
    Easy installation
    Cost: one postcard
    Benefits

    View Slide

  48. Drawbacks
    Only use this for small to medium sized apps
    Can consume quite a bit a of disk space while backing up
    Application has credentials to access the backups
    No restore options

    View Slide

  49. https://docs.spatie.be/laravel-backup

    View Slide

  50. Make Laravel 5.3 compatible
    Use Laravel 5.3’s native notification capabilities
    Easier passing of arguments to the db dumpers
    PHP 7
    Future plans

    View Slide

  51. Do not rely solely on backups of your service provider
    Take care of backups yourself as well
    Many options available both free and paid
    Laravel Backup can help you out (cost: one postcard)
    Summary

    View Slide

  52. QUESTIONS?

    View Slide

  53. Thank you!
    https://joind.in/talk/8824f
    https://speakerdeck.com/freekmurze/backing-up-with-laravel
    https://murze.be
    https://spatie.be/opensource

    View Slide