0

I am working on a project in Laravel where I have to take backup of the database (mySQL).

I have learned to take backup of entire database or selected tables. But the challenge for me is to take backup of only some rows.

Here is the scenario:

There are 4 tables

  • users
  • posts
  • tags
  • post_tag

Their relations

  • user hasMany posts and each post belongsTo a user
  • post hasMany Tags and a Tag hasMany posts

If if initiate backup of a user (where userId = 1), Then I should get a backup file containing all the four tables mentioned above with data related to userId = 1.

Also, how to restore the data?

Updates

It is a role based application. (There are 2 roles editor and author)

Editor has the privilege to backup and restore data of author.

aswal94
  • 145
  • 2
  • 14
  • it is bug free and easy to use and change. https://stackoverflow.com/questions/58207052/how-to-backup-export-the-connected-database-database-sql-file-in-laravel/67285015#67285015 – pankaj Apr 27 '21 at 14:30

1 Answers1

1

If this is an server admin only operation, you may use iseed to generate seeding files from your existing data in database. The restoration would be easy through Laravel's database seeding.

If this is a user operation, then you would probably need to program your own import / export feature. You may use a combination of fopen('php://output') with fputcsv for the database to csv export. Then you may reference some available tutorials to write the csv to database import with Eloquent.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
  • I need a method which can be implemented on button click.....(please check the question i've updated it).....also... if it's possible using iseed could you please guide me. – aswal94 Oct 14 '19 at 13:43
  • Updated my answer. Please check. – Koala Yeung Oct 15 '19 at 02:30
  • Which would be the best approach: Joining all the tables of that user then saving or creating multiple files or creating a query list – aswal94 Oct 15 '19 at 03:06
  • If you, on import, are simply going to recreate everything with the same ID and primary keys, I don't see a need to join the tables before export. – Koala Yeung Oct 15 '19 at 03:20
  • If, however, you need to import without the keys, then you should probably aggregate a tree-like structure and export as JSON. Joining would be preferable. – Koala Yeung Oct 15 '19 at 03:23
  • Thanks for the guidance and to be patient. – aswal94 Oct 15 '19 at 03:34