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

[sourcecode language=”bash”]
#!/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
[/sourcecode]

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

Terbaru

  • Inilah Cara Dapat Saldo Cool Lady Tanpa Undang Teman, Ternyata Begini Trik Rahasianya!
  • Inilah Cara Main Watermelon Merge Fun Biar Dapat Saldo DANA, Apakah Benaran Membayar?
  • Inilah Kenapa Stok Penarikan Free Reels Sering Habis dan Cara Mengatasinya Agar Saldo Cair!
  • Inilah Alasan Kenapa Telat Lapor SPT 2026 Nggak Bakal Kena Denda, Cek Aturan Lengkapnya!
  • Inilah Cara Mematikan Koreksi Otomatis di WhatsApp Agar Nggak Salah Ketik Lagi
  • Apa itu Bujang Inam? Inilah Alasan Kenapa Kata Ini Jadi Makian Paling Kasar dalam Budaya Medan dan Batak!
  • Inilah Kenapa Link FF Kipas My ID Verify UID Beta Testing Sering Gagal dan Cara Menghadapinya
  • Gini Caranya Mulai Dropshipping Pake AI di Tahun 2026, Auto Cuan Tanpa Ribet!
  • Baterai Smartwatch Boros? Inilah Caranya Biar Baterai Smartwatch Kalian Bisa Awet Berhari-hari!
  • Belum Tahu? Inilah Fakta Cahaya Misterius di Lampung yang Ternyata Sampah Roket China CZ-3B!
  • Inilah Roblox Mod APK 2026, Fitur Premium Jadi Gratis dan Cara Pasangnya yang Perlu Kalian Tahu!
  • Inilah Panduan Lengkap UTUL UGM 2026, Cari Tahu Jadwal, Biaya Pendaftaran, Sampai Info Uang Pangkal IPI di Sini!
  • Inilah IGRS di Roblox dan Steam, Ternyata Ini Alasan Komdigi Kasih Aturan Ketat!
  • Inilah Makna Mendalam Regina Caeli: Kenapa Umat Katolik Wajib Doa Ratu Surga Selama Masa Paskah?
  • Inilah Kronologi Lengkap Kecelakaan Truk TNI di Kalideres: Simak Fakta dan Status Sopirnya Sekarang!
  • Inilah Rahasia Ambil Ide Youtube Lain, Tapi Konten Kalian Nggak Terlihat Membosankan
  • Studi Kasus SEO: Ellevest Dapat 14 Ribu Pengunjung Organik Per Bulan dengan Strategi SEO Niche dan Otomasi AI
  • Inilah Rahasia ReciMe, Potensi Sukses dari Aplikasi Resep Sederhana
  • Perkuat Kemandirian Industri Alat Kesehatan Nasional, Astra Komponen Indonesia Hadirkan Alat Kesehatan Berbasis Teknologi Digital
  • Vidrush, Solusi Produksi Video massal buat Channel Faceless
  • Inilah Higgsfield AI Audio, Trik Canggih Buat Voice Cloning dan Dubbing Video Youtube Otomatis!
  • Everything You Need to Know About Project X and the Rumored AI-Powered Remaster of The Sims 4
  • Inilah Trik Cuan dari Instagram Jadi Affiliator, Tapi Tanpa Perlu Jualan Produk!
  • Inilah 7 Ide Channel YouTube Aneh Tapi Sederhana yang Bisa Kalian Mulai Sekarang Juga!
  • Apa itu Umroh & Keutamaannya: Inspirasi dari pergiumroh.com
  • Belum Tahu? Gini Caranya Dapat Bisnis Sukses Cuma dari Clipping Video Pake AI
  • Inilah Rahasia Perbaiki Algoritma Video YouTube yang Mulai Sepi
  • Kenapa Cicilan di Bank Syariah Itu Tetap?
  • Inilah 7 Produk Digital Paling Realistis untuk Kalian yang Mau Jualan Online Tahun Ini!
  • Inilah 4 Strategi Memilih Niche SEO Terbaik Supaya Blog Kalian Cepat Ranking
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Clear Copilot Memory in Windows 11 Step by Step
  • How to Use Orbax Checkpointing with Keras and JAX for Robust Training
  • How to Automate Any PDF Form Using the Power of Manus AI
  • How to Training Your Own YOLO26 Object Detection Model!
  • How to Build a Full-Stack Mobile App in Minutes with YouWare AI
  • How to Create Consistent Characters and Cinematic AI Video Production with Seedance
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

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