Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Backup MySQL tabel per tabel dengan rotasi otomasi

Posted on January 12, 2014

Berikut ini adalah MySQL backup script yang dapat mengambil tabel dengan backup tabel (file cadangan individu masing-masing tabel database masing-masing) dalam format terkompresi. Hal ini juga memberikan rotasi otomatis dari file backup yang lama. Script cadangan menangani tabel InnoDB dan MyISAM secara terpisah.

Anda harus mengatur variabel-variabel berikut sebelum menjalankan skrip backup.

DB_User

Database user yang memiliki akses ke semua database dan tabel. Saya menggunakan “root” untuk penyebaran saya.

DB_PASS

Password dari pengguna di atas, diawali dengan “-p”. Misalnya jika password yang Rahasia, maka Anda harus menulis password sebagai “-pSecret”.

BAKUP_LOG

File untuk yang log cadangan akan ditulis. Ini harus dapat ditulis oleh user yang menjalankan script.

BASE_BAK_FLDR

Folder backup. Ini harus dapat ditulis oleh user yang menjalankan script.

RM_FLDR_DAYS

Contoh

Rotasi backup +30 artinya backup tiap 30 hari sekali. Script ditulis oleh Mohammed Salih/HowToForge.com

#!/bin/bash
# Database Backup script.
# Created By:    Mohammed Salih
#                 Senior System Administrator
#                Date: 21/06/2007
#
# Database credentials
DB_USER=root
#Please append password in the xxxxx section below, note that there is
# no space between -p and xxxxx
DB_PASS="-pxxxxxxx"
# Get list of Databases except the pid file
DBS_LIST=$(echo "show databases;"|mysql -u $DB_USER $DB_PASS -N)
# Log file
BAKUP_LOG=/backup/log/db-backup.log
# Backup Base directory
BASE_BAK_FLDR=/backup/db
# Backup rotation period.
RM_FLDR_DAYS="+30"
# From here, only edit if you know what you are doing.
index=0
# Check if we can connect to the mysql server; otherwise die
if [ ! "$(id -u -n)" = "mysql" ]; then
        echo -e "Error:: $0 : Only user 'mysql' can run this script"
        exit 100
fi
PING=$(mysqladmin ping -u $DB_USER $DB_PASS 2>/dev/null)
if [ "$PING" != "mysqld is alive" ]; then
        echo "Error:: Unable to connected to MySQL Server, exiting !!"
        exit 101
fi
# Backup process starts here.
# Flush logs prior to the backup.
mysql -u $DB_USER $DB_PASS -e "FLUSH LOGS"
# Loop through the DB list and create table level backup,
# applying appropriate option for MyISAM and InnoDB tables.
for DB in $DBS_LIST; do
    DB_BKP_FLDR=$BASE_BAK_FLDR/$(date +%d-%m-%Y)/$DB
    [ ! -d $DB_BKP_FLDR ]  && mkdir -p $DB_BKP_FLDR
    # Get the schema of database with the stored procedures.
    # This will be the first file in the database backup folder
    mysqldump -u $DB_USER $DB_PASS -R -d --single-transaction $DB | \
            gzip -c > $DB_BKP_FLDR/000-DB_SCHEMA.sql.gz
    index=0
    #Get the tables and its type. Store it in an array.
    table_types=($(mysql -u $DB_USER $DB_PASS -e "show table status from $DB" | \
            awk '{ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
    table_type_count=${#table_types[@]}
    # Loop through the tables and apply the mysqldump option according to the table type
    # The table specific SQL files will not contain any create info for the table schema.
    # It will be available in SCHEMA file
    while [ "$index" -lt "$table_type_count" ]; do
        START=$(date +%s)
        TYPE=${table_types[$index + 1]}
        table=${table_types[$index]}
        echo -en "$(date) : backup $DB : $table : $TYPE "
        if [ "$TYPE" = "MyISAM" ]; then
            DUMP_OPT="-u $DB_USER $DB_PASS $DB --no-create-info --tables "
        else
            DUMP_OPT="-u $DB_USER $DB_PASS $DB --no-create-info --single-transaction --tables"
        fi
        mysqldump  $DUMP_OPT $table |gzip -c > $DB_BKP_FLDR/$table.sql.gz
        index=$(($index + 2))
        echo -e " - Total time : $(($(date +%s) - $START))\n"
    done
done
# Rotating old backup. according to the 'RM_FLDR_DAYS'
if [ ! -z "$RM_FLDR_DAYS" ]; then
    echo -en "$(date) : removing folder : "
    find $BASE_BAK_FLDR/ -maxdepth 1 -mtime $RM_FLDR_DAYS -type d -exec rm -rf {} \;
    echo
fi

Lokasi backup di atur pada: $BKP_BASE_FLDR/01-01-2007/bigdb

Terbaru

  • Rumor Galaxy Z Flip8 Jadi Seri Terakhir: Mengapa Samsung Mungkin Berhenti Bikin HP Lipat Clamshell?
  • Jangan Asal Lap! Inilah Cara Membersihkan Layar TV LED yang Benar Agar Tidak Rusak
  • Realme Narzo 100x 5G Segera Rilis: Baterai 8.000mAh, Layar 144Hz, dan Sertifikasi Militer
  • Waspada Penipuan Akun Mobile Legends: Kenali Pentingnya Marketplace Resmi dan Status PSE
  • Review DJI Osmo Pocket 4P: Kamera Saku Lensa Ganda yang Bikin Kamera Pro Kelihatan Ribet!
  • Samsung Bisa “AirDrop” ke iPhone? Cek Cara Pakai Fitur Quick Share Terbaru Ini!
  • Vivo X Fold 6 Bakal Masuk Indonesia? Speknya Gila, Baterai 7.000 mAh & Kamera 200 MP!
  • Bocoran iPhone Air 2: Akhirnya Apple Nggak Cuma Jual Tipis, Tapi Juga Baterai Awet dan Kamera Oke!
  • Google Search Console rilis fitur Social Media Property, bisa pantau TikTok & Instagram
  • Hati-hati! 22 Platform Digital Terancam Diblokir Pemerintah, Cek Daftar Hotel dan Maskapainya di Sini
  • Baterai 7.000mAh & Standar Militer, Inilah Moto G77 Power
  • Gila Sih! Vivo Rilis HP Baterai 8.000 mAh di 2026, Nggak Perlu Ribet Bawa Powerbank Lagi?
  • Gak Perlu HP Mahal buat Pake AI: Intip Fitur Canggih di Samsung Galaxy A37 dan A57 yang Bikin Hidup Lebih Gampang
  • Bocoran Galaxy Unpacked Juli 2026: Z Fold 8 & Z Flip 8 Bakal Fokus ke On-Device AI, Makin Canggih atau Cuma Gimmick?
  • Duel Tablet 2 Jutaan: ADVAN Tab Sketsa 3 vs itel VistaTab 30GT, Mana yang Lebih Worth It buat Kerja?
  • Review Asus ExpertBook PM5 G2: Laptop Bisnis Rasa AI, Ringan tapi Gak Kaleng-kaleng
  • Review Redmi Pad 2 9.7 4G: Tablet Murah yang Bisa Pakai Kartu SIM, Worth It Nggak?
  • Review JBL Quantum Series: Inilah Alasan Kenapa Audio Berkualitas Bisa Bikin Kamu Menang Main Game
  • Review Vivo Y500: HP Baterai Jumbo 8.100mAh yang Bikin Nggak Perlu Cari Colokan Lagi!
  • Oppo Reno16 Series Resmi Masuk Indonesia: Kamera 200MP dan Fitur AI yang Bikin Konten Jadi Sat-set!
  • Jangan Tergiur Harga Murah! Inilah Risiko Fatal Membeli iPhone WiFi Only yang Wajib Kamu Tahu
  • Cara Membuat Caption Berbeda di Setiap Foto Carousel Instagram, Fitur Baru yang Bikin Konten Makin Kece!
  • Review Lenovo Idea Tab Pro Gen 2: Tablet Android Rasa Laptop dengan Fitur AI Canggih
  • Inilah Rekomendasi HP Midrange Terbaik Selain Xiaomi 17T dengan Kamera Potret Premium
  • Inilah Bocoran Spek dan Harga Nubia Neo 5 GT & REDMAGIC 11S Pro: HP Gaming Anti Throttling!
  • Rekomendasi HP OPPO Termurah 2026: Cek Perbandingan A5i, A3X, dan A6x!
  • Cara Memilih Water Heater Listrik Hemat Energi dan Aman untuk Smart Home
  • Strava Kena PPN di Indonesia: Langganan Premium Jadi Mahal? Cek Faktanya di Sini!
  • Viral Petani Tuban “Numpang” Drone, Emang Bisa Ya Drone Angkat Manusia? Ini Faktanya!
  • Epson Rilis SC-F20030 di Surabaya Printing Expo 2026: Solusi Cetak Tekstil Digital yang Makin Ngebut
  • Deploy Nginx Rootful Container with Podman
  • How to Sandboxing Browser on Linux Desktop with Flatpak
  • How to Hardening Journald on Linux Server (Fedora/AlmaLinux)
  • Block Bad USB on Linux Server with USBGuard
  • How to Secure NetworkManager on Fedora/AlmaLinux
  • How to Automate Your Entire SEO Strategy Using a Swarm of 100 Free AI Agents Working in Parallel
  • How to create professional presentations easily using NotebookLM’s AI power for school projects and beyond
  • How to Master SEO Automation with Google Gemini 3.1 Flash-Lite in Google AI Studio
  • How to create viral AI video ads and complete brand assets using the Claude and Higgsfield MCP integration
  • How to Transform Your Mac Into a Supercharged AI Assistant with Perplexity Personal Computer
  • Langkah Nyata Novotel dan WWF Prancis Jaga Ekosistem Laut Lewat Meja Makan
  • Bedah Citra Pearly Glow UV Moisturizer: Benarkah Efektif Mencerahkan atau Sekadar Melembapkan?
  • Kenapa Harus Pilih Facial Wash Non-SLS? Intip Rekomendasi Wardah untuk Jaga Skin Barrier Tetap Sehat
  • Lagi rame soal penggeledahan Cafe de’Clan Signature di Cipete, siapa sebenarnya Ferry Yanto Hongkiriwang?
  • Lagi rame bahas bapaknya Dhot Design, kok nggak muncul-muncul? Ternyata ini jawabannya

©2026 emka.web.id | Design: Newspaperly WordPress Theme