How Can We Help?

自動備份資料庫、找出佔用大量容量的資料夾

You are here:
< All Topics

原因

天有不測風雲,就怕不小心資料庫掛了,勤加備份就是買份保險

我們可以利用簡單的 Shell Script + Crontab 自動排程做定期備份

再利用 Shell Script 自動刪除過久的備份檔

 

解決方案

製作備份用 Shell Script
date=$(date +"%Y-%m-%d_%H:%M:%S")
db='GAPL'
backup_path="/opt/odoo_apps/pgdump"
filename="$backup_path/${db}_$date.sql"

# Stop OpenERP Server
/etc/init.d/odoo stop

# Taking Dump of selected DB
sudo -H -u postgres bash -c "pg_dump --format=c --no-owner $db>$filename"

# Start OpenERP Server
/etc/init.d/odoo start

exit 0
製作刪除過時備份檔用 Shell Script

建立可執行的 shell script

刪除超過 10 日的檔案
find -mtime +10 -type f -name "檔名規則" -exec rm {} \;

也可用另一種寫法達到一樣目的

find /資料夾/* -name "檔名規則" -mtime +10 -delete
設定 Crontab 自動排程
編輯 /etc/crontab

每五分鐘執行一次
*/5 * * * * root xxxxx.sh
每天凌晨 12 點執行一次
00 12 * * * rootxxxxx.sh
找出佔用大量容量的資料夾
顯示一層資料夾
du -B M ./ --max-depth=1 | sort -g
顯示兩層資料夾
du -B M ./ --max-depth=2 | sort -g
Table of Contents