MySQL Back-up: mysqldump with each database in its own SQL File

0 0
Read Time:2 Minute, 2 Second

Take a mysqldump back-up to separate files

To take a back-up, run the mysqldump tool on each available database.

$ mysql -N -e 'show databases' | while read dbname; do mysqldump --complete-insert --routines --triggers --single-transaction "$dbname" > "$dbname".sql; done

The result is a list of all your database files, in your current working directory, suffixed with the .sql file extension.

$ ls -alh *.sql

-rw-r--r-- 1 root root  44M Aug 24 22:39 db1.sql
-rw-r--r-- 1 root root  44M Aug 24 22:39 db2.sql

If you want to write to a particular directory, like /var/dump/databases/, you can change the output of the command like this.

$ mysql -N -e 'show databases' | while read dbname; do mysqldump --complete-insert --routines --triggers --single-transaction "$dbname" > /var/dump/databases/"$dbname".sql; done

Mysqldump each database and compress the SQL file

If you want to compress the files, as you’re taking them, you can run either gzip or bzip on the resulting SQL file.

$ mysql -N -e 'show databases' | while read dbname; do mysqldump --complete-insert --routines --triggers --single-transaction "$dbname" > "$dbname".sql; [[ $? -eq 0 ]] && gzip "$dbname".sql; done

The result is again a list of all your databases, but gzip‘d to save diskspace.

$ ls -alh *.gz

-rw-r--r--  1 root root  30K Aug 24 22:42 db1.sql.gz
-rw-r--r--  1 root root 1.6K Aug 24 22:42 db1.sql.gz

This can significantly save you on diskspace at the cost of additional CPU cycles while taking the back-up.

Import files to mysql from each .SQL file

Now that you have a directory full of database files, with the database name in the SQL file, how can you import them all again?

The following for-loop will read all files, strip the “.sql” part from the filename and import to that database.

Warning: this overwrites your databases, without prompting for confirmation. Use with caution!

$ for sql in *.sql; do dbname=${sql/.sql/}; echo -n "Now importing $dbname ... "; mysql $dbname < $sql; echo " done."; done

The output will tell you which database has been imported already.

$ for sql in *.sql; do dbname=${sql/.sql/}; echo -n "Now importing $dbname ... "; mysql $dbname < $sql; echo " done."; done 

Now importing db1 ...  done.
Now importing db2 ...  done.

These are very simple one-liners that come in handy when you’re migrating from server-to-server.

Loading

Happy
Happy
100 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

  • Related Posts

    Average Rating

    5 Star
    0%
    4 Star
    0%
    3 Star
    0%
    2 Star
    0%
    1 Star
    0%

    Incase you missed

    Alphabetical list of some Kenyan banks along with their SWIFT codes

    Alphabetical list of some Kenyan banks along with their SWIFT codes

    [Resource] : Comprehensive List of Equity Bank Codes Across Kenya by Region

    [Resource] : Comprehensive List of Equity Bank Codes Across Kenya by Region

    Exploring ETIMS: Benefits and Impact on Modern Businesses

    Exploring ETIMS: Benefits and Impact on Modern Businesses

    Filing Your KRA 2024 Annual Returns: What You Need to Know

    Filing Your KRA 2024 Annual Returns: What You Need to Know

    [Continuation} : five current global situations and how making Physics and Geography compulsory in education could provide solutions

    [Continuation} : five current global situations and how making Physics and Geography compulsory in education could provide solutions

    [Continuation]: Current Challenges in Making Physics and Geography Compulsory

    [Continuation]: Current Challenges in Making Physics and Geography Compulsory

    [Resource] : Why Physics and Geography Should Be Compulsory Like Mathematics in Education

    [Resource] : Why Physics and Geography Should Be Compulsory Like Mathematics in Education

    Explainer : What Proxies are and their usage

    Explainer : What Proxies are and their usage

    MPESA consumer, secret and passkeys {Safaricom M-pesaWeBPortal} for API integration {Daraja 2.0}

    MPESA consumer, secret and passkeys {Safaricom M-pesaWeBPortal} for API integration {Daraja 2.0}

    [LINKTREE] 2024 PAST PAPERS , NOTES ,RESOURCE,REVISION,EXAMINATIONS

    [LINKTREE] 2024 PAST PAPERS , NOTES ,RESOURCE,REVISION,EXAMINATIONS

    2024 PAST PAPERS , NOTES ,RESOURCE,REVISION,EXAMINATIONS

    2024 PAST PAPERS , NOTES ,RESOURCE,REVISION,EXAMINATIONS

    Shipbrokers Breach of Authority

    Shipbrokers Breach of Authority

    Explain classification of law

    Explain classification of law

    Maritime Terms, Abbreviations and Acronyms [Shipping Terms – Searchable]

    Maritime Terms, Abbreviations and Acronyms [Shipping Terms – Searchable]

    Maritime Terms, Abbreviations and Acronyms [ Shipping Terms]

    Maritime Terms, Abbreviations and Acronyms [ Shipping Terms]

    KCB BANK CODES ACROSS KENYA BY REGION tabular

    KCB BANK CODES ACROSS KENYA BY REGION tabular

    Optimizing Container Stowage Plan for Efficient Cargo Placement on Ships

    Optimizing Container Stowage Plan for Efficient Cargo Placement on Ships

    Choosing Between Managed and Unmanaged Services

    Choosing Between Managed and Unmanaged Services

    Differences Between Managed and UnManaged Services

    Differences Between Managed and UnManaged Services

    How to check KRA PIN using BRS (Ecitizen)

    How to check KRA PIN using BRS (Ecitizen)

    HTML ERROR MESSAGES

    HTML ERROR MESSAGES

    DISCUSS THE SYSTEM OF COURTS IN KENYA

    DISCUSS THE SYSTEM OF COURTS IN KENYA

    GLOSSARY ON INTERNET TERMS

    GLOSSARY ON INTERNET TERMS

    KEYBOARD SHORTCUTS

    KEYBOARD SHORTCUTS

    NESTICT LMS: INTERNET NOTES

    NESTICT LMS: INTERNET NOTES

    REPORTED SPEECH

    REPORTED SPEECH

    [Explainer]: NVMe storage, SSD (SATA SSD), and HDD

    [Explainer]: NVMe storage, SSD (SATA SSD), and HDD

    SSD vs HDD: What’s the difference?

    SSD vs HDD: What’s the difference?

    Enabling Hyper-V on Windows 10

    Enabling Hyper-V on Windows 10

    Discussion : Windows 10 Virtualization

    Discussion : Windows 10 Virtualization

    BLUETOOTH LAN NETWORK

    BLUETOOTH LAN NETWORK

    PROS CONS AND WAYS OF INVESTING IN BITCOIN

    PROS CONS AND WAYS OF INVESTING IN BITCOIN

    What is a cloud service provider

    What is a cloud service provider

    Application Service Provider (ASP) Meaning

    Application Service Provider (ASP) Meaning

    POSTAL CODES – Updated as at July 2024 – PDF

    POSTAL CODES – Updated as at July 2024 – PDF

    POSTAL CODES – Updated as at July 2024

    POSTAL CODES – Updated as at July 2024

    Check your disk space use with the Linux df command

    Check your disk space use with the Linux df command

    Best Places to get Accounting Internship Opportunities

    Best Places to get Accounting Internship Opportunities

    Top Kenyan Based Companies to apply for internship

    Top Kenyan Based Companies to apply for internship

    Credit Scores Quickk FAQs

    Credit Scores Quickk FAQs

    Credit Score Ranges: What Do They Mean?

    Credit Score Ranges: What Do They Mean?

    RANDOMIZING PHONE NUMBERS IN EXCEL

    RANDOMIZING PHONE NUMBERS IN EXCEL

    [Updated 2024] – Passport Application FOR CHILDREN ONLY(PERSONS UNDER 18 YEARS)

    [Updated 2024] – Passport Application FOR CHILDREN ONLY(PERSONS UNDER 18 YEARS)

    [Updated 2024] -Passport Application FOR ADULTS ONLY-PERSONS OVER 18 YEARS

    [Updated 2024] -Passport Application FOR ADULTS ONLY-PERSONS OVER 18 YEARS

    Understanding Tires Load Index vs Load Range on Car Tires

    Understanding Tires Load Index vs Load Range on Car Tires

    Computer Bus: Video

    Computer Bus: Video

    Computer Bus

    Computer Bus

    Types of Computer Network Topology Explained With Diagrams

    Types of Computer Network Topology Explained With Diagrams

    The Functions of PCI Slots

    The Functions of PCI Slots

    Complementary Metal Oxide Semiconductor – CMOS

    Complementary Metal Oxide Semiconductor – CMOS

    The Computer Motherboard and Its Components Explained

    The Computer Motherboard and Its Components Explained

    AI in Education is Best Experiences

    AI in Education is Best Experiences

    Harnessing the Power of Wind Energy

    Harnessing the Power of Wind Energy

    The Golden Gate’s Timeless Majesty

    The Golden Gate’s Timeless Majesty

    Rise of Competitive Video Gaming

    Rise of Competitive Video Gaming

    Ultimate Sports Ground Experience

    Ultimate Sports Ground Experience

    Global Ocean Cleanup Initiatives

    Global Ocean Cleanup Initiatives

    The Enduring Appeal of Gaming Classics

    The Enduring Appeal of Gaming Classics

    The Art of Mindful Eating

    The Art of Mindful Eating

    Classic Video Games Making a Comeback

    Classic Video Games Making a Comeback