Header Ads Widget

Viết script Backup & Restore dữ liệu trên VPS

Tự viết một Shell script để thực hiện công việc backup để khi nào cần backup, bạn chỉ cần gõ một câu lệnh nào đó mà bạn tự đặt ra thay vì sử dụng các lệnh thủ công.

Những việc cần làm khi quản lý máy chủ chạy web, tạo file backup all databases Mysql, khôi phục Mysql từ file đã backup

Tạo Script Shell để thực hiện việc sao lưu mysql file vvlbackup với nội dung

#! /bin/sh
# Folder chua file backup
folder_save_backup=$1
ngay=$(date +%d-%m-%Y) ## Dinh hinh cau truc ngay thang kieu DD-MM-YYYY (31/05/2014)
file_name="backup_data_$ngay" # Ten file se co dang backup-31-05-2014
  
####### backup mysql ###########
echo "Dang tien hanh sao luu co so du lieu mysql"
# Thuc hien viec backup mysql va luu vao thu muc $folder_save_backup
mysqldump -h localhost -u root -pDB_PASS--all-databases | gzip > $folder_save_backup/backup_mysql_$ngay.sql.gz
# Hien thi thong bao sau khi backup xong
echo
echo "SAO LUU THANH CONG!"
echo "Da sao luu du lieu mysql tai thu muc: $folder_save_backup"
####### backup data ###########
# Folder chua du lieu
backup_folder=$2
# Quy trinh backup
echo "Dang tien hanh backup du lieu tai $backup_folder va se duoc gui den $folder_save_backup"
echo
zip -r $folder_save_backup/$file_name cd $backup_folder > /dev/null
# Hien thi thong bao sau khi backup xong
echo
echo "BACKUP THANH CONG!"
echo "Ten file backup: $file_name"
echo "File backup da duoc luu tai: $folder_save_backup"
ls -al $folder_save_backup

Lưu file này lại và CHMOD:

chmod 755 /bin/vvlbackup
Tại cửa sổ command gõ lệnh:
vvlbackup thu_muc_sao_luu_file_backup thu_muc_du_lieu_code_sao_luu
thu_muc_sao_luu_file_backup : là folder sẽ sao lưu file backup mysql
Ví dụ: /home/chiaseaz/thu_muc_du_lieu_code_sao_luu : là folder dữ liệu code sẽ được sao luu,
Tạo Script Shell để thực hiện việc khôi phục mysql
Đầu tiên là tạo ra một file với tên vvlrestore trong thư mục /bin với nội dung
#! /bin/sh
#
# Thiet lap bien (variables) can thiet
# Ngay thang sao luu
date_restore=$1
# Folder chua file sao luu
folder_restore=$2
 
####### restore mysql ###########
echo "Dang tien hanh khoi phuc co so du lieu mysql"
#Giai nen giu nguyen file cu
gunzip -c $folder_restore/backup_$date_restore.sql.gz > $folder_restore/backup_$date_restore.sql
#restore all databases
echo "Vui long nhap mat khau mysql"
mysql -u root -ppassword test < $folder_restore/backup_mysql_$date_restore.sql
####### restore data ###########
unzip -o $folder_restore/backup_data_$date_restore.zip -d $folder_restore/public_html
# Den thu muc chua du lieu
cd $folder_restore/public_html$folder_restore/public_html
# Di chuyen du lieu den public_html
mv * $folder_restore/public_html
# Xoa cay thu muc ao phat sinh khi backup
rm -rf $folder_restore/public_html/home
# Hien thi thong bao sau khi backup xong
echo
echo "KHOI PHUC THANH CONG!"
Lưu file này lại và CHMOD cho nó:
chmod 755 /bin/vvlrestore

Tại cửa sổ command gõ lệnh:
vvlrestore ngay-thang-nam thu_muc_chua_file_backup
ngay-thang-nam : là thời diểm muốn khôi phục, Ví dụ: 30-12-2015
thu_muc_chua_file_backup : là folder chứa file backup mysql đã nén ở bước trên, Ví dụ: /home/chiaseaz

Hệ thống sẽ hỏi mật khẩu mysql của root và bạn chỉ cần nhập mật khẩu vào là xong.

Tạo cronjob để chạy theo lịch định sẵn

Lịch backup theo thời gian
Ví dụ: backup 1 phút 1 lần
0/1 * * * * vvlbackup /home/chiaseaz /home/chiaseaz/public_html
Backup mỗi ngày vào lúc 3h sáng
0 3 * * * vvlbackup /home/chiaseaz /home/chiaseaz/public_html
Có thể sử dụng công cụ Crontab Code Generator để hỗ trợ tạo crontab

Nhận xét