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

  • Cara Menambah View Story Instagram Gratis Biar Nggak Sia-sia
  • Kenapa View TikTok Kalian Mentok di Angka Kecil dan Cara Ngatasinnya
  • Imbas Pesatnya Laporan CVE, Ubuntu Bakal Rilis Kernel Update Tiap Dua Minggu
  • Cara Gampang Cari Kata di Google Sheets Pakai Laptop sama HP
  • Kabar Terbaru Antigravity SDK: Sekarang Bisa Pakai Model Lokal Kayak Gemma 4 26B Tanpa Perlu Internet
  • Kabar Terbaru Model K2 Horizon Dirilis: MoVA 36B EXL3 Buat Kalian yang Punya GPU Beragam
  • Ini Alasan Kenapa Kalian Harus Cek Masa Dukungan HP Android Kalian Sekarang Juga
  • Cara Cek Followers Baru Asli atau Bot Tanpa Harus Ngitung Satu-Satu
  • Claude Opus 5.5 Baru Rilis, Bikin AI Kalian Jadi Jauh Lebih Powerfull!
  • Update Kondisi Market Bitcoin BTC/USDT Per 23 September 2026, Beli atau Jual Nih?
  • Paham Semua Keluarga Model AI Gemma 4
  • Rumor Laptop Android Ternyata Bener, Ini Daftar 4 Laptop Android dari Google!
  • LCOS Lagi Naik Daun: OS Buatan Lunduke Tembus Peringkat 12 DistroWatch dan Punya Kernel Sendiri
  • Cara Pindah Data Android ke iPhone Tanpa Reset iPhone
  • Valve: Steam Deck 2 Tetap di Develop, Meski RAM Naik Harga Gila-gilaan
  • Apa itu Dumpling? dari Sejarah Sampai Tren Saat ini
  • Jadwal Event Cosplay dan Pop Culture Oktober 2026 yang Wajib Kalian Pantau
  • Cara Ganti Nama Facebook di Android, iPhone, Laptop, dan Facebook Lite Terbaru
  • Apa saja yang baru di WordPress 7.1.1?
  • Pilih Mana: XAMPP, Laragon, atau Herd Buat Belajar PHP?
  • Kenapa Views Reels Kalian Tiba-tiba Anjlok? Ini Penyebab dan Cara Ngatasinya
  • Android Bench versi 2.0 Dirilis, Apa saja Fitur Barunya?
  • MagicOS 11 dari Honor Sudah Dirilis Nih, Ini Cara Cek Smartphonemu Dapet Update Atau Nggak
  • Samsung Galaxy Z Fold 8 Jadi Makin Populer Karena iPhone Duo dirilis :)
  • Update Roku Terbaru: Redesain Tampilan & Tambah Apps dan Subscription Bundle
  • Baru! Android Auto Kasih Update ‘Custom Driving Avatar’ di Google Maps
  • Inilah Alasan Kenapa Suno Batasi Download, Mau Pindah Platform atau Terus Langganan?
  • Mengenal Apa itu Ansible
  • Cara Memilih Paket Diamond Mobile Legends Sesuai Kebutuhan dan Budget
  • Laptop Bisnis dengan Fitur Perlindungan BIOS Terbaik dari Ancaman Siber
  • Canonical: Ubuntu coming soon to Snapdragon X2 Series platforms
  • Finally, wolfSSL adds post-quantum algorithms!
  • How to Run Gemma Embedding Models Using Docker
  • Deploy Nginx Rootful Container with Podman
  • How to Sandboxing Browser on Linux Desktop with Flatpak
  • Pruna AI Release their Qwen-Image 2.1 LoRA Adapter, 6x Faster than Regular Qwen
  • The Rise and Fall of the Coding Holy Grail: Is Stack Overflow Just AI Fuel Now?
  • Zyphra is The Pioneer, Why Everyone is Suddenly Obsessed with AMD and Not Just Nvidia Anymore
  • Stop Trusting Your Prompts to Save Your Business Data
  • How to Automate Your Entire SEO Strategy Using a Swarm of 100 Free AI Agents Working in Parallel
  • Kabar duka Guruh Soekarnoputra wafat, banyak yang tanya sakit apa dan soal kehidupan pribadinya
  • Siapa Nuzran Joher? Ketua Ombudsman RI Baru yang Banyak Dicari Warga, Ternyata Ini Rekam Jejaknya
  • Profil Wisnu Hani Putranto, Kalapas Cibinong yang Lagi Jadi Sorotan Usai Sidak Ombudsman
  • Beneran orang pertama di Indonesia beli iPhone 18 Pro Max? Sosok Delvin Sucahya lagi rame dibahas netizen
  • Mengenal Timothy Ivan Triyono, dari Staf Khusus KSP sampai jadi Deputi di Bakom RI

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