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

  • Apa Itu Extend Volume? Ini Cara Memperluas Drive C di Windows 11
  • Ini Trik AFK Fish It Roblox Pakai LDCloud, Auto Panen Ikan Tanpa Bikin HP Panas!
  • Apa itu Game Zenless Zone Zero (ZZZ) HoYoVerse? Ini Cara Mainnya
  • Cuma Kurang 1 Rupiah! Misteri Lucky Draw Akulaku Rp300 Ribu, Bisa Cair Nggak Sih?
  • Video Melolo Cuma Layar Hitam? Ini Trik Ampuh Mengatasinya, Pasti Berhasil!
  • Mau Simpan Video Twitter dan TikTok Tanpa Aplikasi? Begini Cara Praktis Pakai VidsSave!
  • Mau Gaji Dolar? Gini Caranya Tembus Kerja di Australia, Jangan Sampai Salah Visa!
  • Belum Tahu? Inilah Fakta MigoReels, Katanya Nonton Drama Bisa Dapat Rp700 Ribu!
  • Apa Itu Event Invite Friends CapCut? Ini Pengertian dan Cara Kerjanya Biar Cuan
  • Apa Itu MJ di FF? Ini Pengertian, Asal-Usul, dan Risiko di Balik Istilah Tersebut
  • Apa Itu Pengertian Penonaktifan SPayLater? Ini Durasi Blokir Akibat Telat Bayar
  • Apa Itu Rasio Gambar Ukuran 1:1 di Canva? Ini Pengertian dan Cara Buatnya
  • Pengiriman Shopee Express Hemat itu Berapa Lama? Ini Pengertian dan Estimasi Sampainya
  • Android 2025: Aplikasi Baru dan Smartphone Impian yang Akan Datang!
  • Apa Itu Google AI Pro Ultra? Ini Pengertian dan Penjelasan Lengkapnya
  • Apa Itu Error Gagal Kirim Nilai RDM 3.1? Ini Pengertian dan Solusi Mengatasinya
  • Facebook Mulai Batasi Link Eksternal Cuma 2 Sebulan! Ini Trik Mengatasinya
  • Cara Nonton Tensura Season 4! Bakal Tayang April 2026 dengan Format 5 Cour
  • Belum Tahu? Inilah Trik Supaya Live TikTok Kalian Aman dan Banjir Cuan
  • Pixel 8 Dapatkan Mode Panorama Baru! Hasil Foto Lebih Luas & Lebih Kreatif
  • Apa Itu AppLocker? Ini Pengertian dan Cara Mengamankan Windows 11 Kalian dari Skrip Jahat
  • Cara Membuat Riwayat Copilot Kamu Tetap Bersih dan Rapi!
  • Game & Aplikasi Android Terbaik Saat Diskon Liburan Natal! Jangan Ketinggalan!
  • Apa Itu Game Prison Escape Journey? Ini Pengertian dan Cara Mainnya untuk Pemula
  • “Listrik Gratis” dari Solar Panel Cuma Mitos?! Ini Sisi Gelap PLTS
  • Samsung Galaxy Z-Fold: Uji Jatuh Bebas yang Mengguncang Keandalan Lipatannya
  • Google One 2026: Apa yang Akan Jadi Fitur Utama dan Harga yang Diharapkan?
  • Apa Itu Error 0x800704f8? Ini Pengertian dan Cara Mengatasinya
  • Android Akhirnya Dapat GPS Darurat di India! Setelah Hampir 10 Tahun
  • Apa Itu GetContact Premium? Ini Pengertian dan Cara Daftarnya
  • Apa itu Cosmic Desktop: Pengertian dan Cara Pasangnya di Ubuntu 26.04?
  • Apa Itu Auvidea X242? Pengertian Carrier Board Jetson T5000 dengan Dual 10Gbe
  • Elementary OS 8.1 Resmi Rilis: Kini Pakai Wayland Secara Standar!
  • Apa Itu Raspberry Pi Imager? Pengertian dan Pembaruan Versi 2.0.3 yang Wajib Kalian Tahu
  • Performa Maksimal! Ini Cara Manual Update Ubuntu ke Linux Kernel 6.18 LTS
  • Prompt AI Tahun Baruan di Bundaran HI
  • Prompt AI Pamer iPhone 17 Pro Max Orange
  • Apa itu GPT 5.2 di Microsoft Copilot? Ini Pengertian dan Keunggulannya
  • Apa Itu Iklan ChatGPT? Mengenal Sponsored Content yang Bakal Muncul di Hasil Pencarian AI
  • Tutorial RAG: Inilah 7 Repository GitHub Terbaik untuk Membangun Sistem AI Sendiri
  • Apa itu CVE-2025-68664? Memahami Celah Keamanan LangGrinch pada LangChain
  • Kronologi Kasus Pencurian Data Karyawan Data Breach Korean Air 2025
  • Ini Kronologi Hack Memalukan Trustwallet di Natal 2025
  • Apa Itu Insiden Eksploitasi Sistem Rainbow Six Siege? Ini Penjelasannya
  • Apa Itu Docker Hardened Images (DHI)? Ini Definisi dan Penjelasannya
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

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