DEV Community

Cover image for Bulk MySQL dump with bash
IRPAN KUSUMA W
IRPAN KUSUMA W

Posted on

2 1

Bulk MySQL dump with bash

[Halo Sobat,]

Kali ini saya akan menyampaikan bagaimana cara melakukan bulk backup database pada mysql.

  • Create file dalam server linux, dengan nama backup.sh. vi backup.sh
  • Paste code berikut ini dalam file backup.sh
#!/bin/sh

#your path for store backup file as filename.sql
fullpath='/path/to/backup'

#parameter for connection database
user="user"
host="your-ip-address"
passwd="pasword"
port="3306"

#name of databases, delimiter with [space]
dbnames="db_01 db_02 db_03"

#looping process for run mysqldump
for db in $dbnames; do
    filename=${db}.sql
    filepath=${fullpath}
    cd $filepath

    /usr/bin/mysqldump -u${user} -p${passwd} --databases ${db} > ${filename}

    # delete file when age is > 2 days
    find $filepath/* -mtime +1 -exec rm -rf {} \;

done
Enter fullscreen mode Exit fullscreen mode
  • Kemudian ubah permission pada file backup.sh chmod +x backup.sh Jika berhasil maka file backup.js akan berubah menjadi hijau bold
  • Selanjutnya untuk menjalankan file tersebut seperti berikut ./backup.sh Tunggu sampai selesai menjalan kan proses, dan cek folder ada sesuai dengan variable fullpath di backup.js
  • Anda juga bisa menjalankan file backup tersebut secara scheduler menggunakan cronjob linux, seperti berikut crontab -e 0 1 0 0 0 /path/to/file/backup.sh

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs