July 31, 2015

Script Tự động sao lưu toàn bộ website WordPress

Thông thường, khi mình muốn di chuyển hoặc sao lưu một website sử dụng WordPress thì sẽ sử dụng plugin Duplicator. Tuy nhiên, có một số trường hợp server bị lỗi, website không truy cập trực tiếp được hoặc bạn muốn sao lưu lại cả một loạt site đang hoạt động trên server thì sao?
Mình có theo dõi các comment trong bài giới thiệu HocVPS Script hoặc trong chuyên mục Hỏi đáp thì thấy nhu cầu này có thật và cũng khá cần thiết. Do đó, mình có viết một chương trình bash script ngắn, sẽ tự động sao lưu lại website WordPress bạn muốn (bao gồm cả files và database).

HocVPS Script Plugin – Backup All WordPress Sites

Để sử dụng bạn chỉ cần sử dụng dòng lệnh sau:
wget http://hocvps.com/scripts/plugin/backupallwp.sh && chmod +x backupallwp.sh && ./backupallwp.sh
Sau đó chương trình sẽ quét toàn bộ website đang hoạt động cùng với thông tin database MySQL để bạn có thể lựa chọn sao lưu. Cuối cùng toàn bộ dữ liệu sẽ được lưu trong thư mục /home/domain.com/private_html/backupallwp với đường link direct để tải về máy hoặc chuyển sang VPS khác.
Lưu ý: 
  1. Để download trực tiếp file về VPS, hãy sử dụng lệnh wget link
  2. Để giải nén bạn hãy sử dụng lệnh unzip file.zip
  3. Toàn bộ database được lưu lại thành 1 file *.SQL, để import bạn hãy sử dụng lệnh mysql -u username -ppassword databasename < database.sql. Lưu ý thay lại username, password, databasename và tên file database
Chương trình này thiết kế để hoạt động tốt với HocVPS Script và WordPress. Tuy nhiên bạn có thể điều chỉnh lại thoải mái cho phù hợp với control panel đang sử dụng.
Trước mắt mình cứ viết dạng plugin như thế này đã, sau này có tích hợp vào HocVPS Script hay không thì sẽ tính sau 😀
Code bash script
#!/bin/bash
# HocVPS Script Plugin - Backup All WordPress Sites
# -------------------------------------------------------------------------
# Description: Create a backup of your WordPress files and database.
# Copyright (c) 2015 Luan Tran <http://hocvps.com>
# -------------------------------------------------------------------------
# Run: wget http://hocvps.com/scripts/plugin/backupallwp.sh && chmod +x backupallwp.sh && ./backupallwp.sh
# -------------------------------------------------------------------------

clear
printf "=========================================================================\n"
printf "         HocVPS Script Plugin - Backup All WordPress Sites\n"
printf "=========================================================================\n"

#Server information
main_site=`cat /etc/hocvps/scripts.conf | grep mainsite | cut -d \" -f 2`
port=`cat /etc/hocvps/scripts.conf | grep priport | cut -d \" -f 2`
serverip=`cat /etc/hocvps/scripts.conf | grep serverip | cut -d \" -f 2`

#Backup path
backup_path=/home/$main_site/private_html/backupallwp
mkdir -p $backup_path

#Loop through /home directory
for D in /home/*; do
 if [ -d "${D}" ]; then #If a directory
  domain=${D##*/} #Domain name
  printf "\n***Found domain $domain\n"
  
  if [ -f "/home/$domain/public_html/wp-config.php" ]; then
   WPDBNAME=`cat /home/$domain/public_html/wp-config.php | grep DB_NAME | cut -d \' -f 4`
   WPDBUSER=`cat /home/$domain/public_html/wp-config.php | grep DB_USER | cut -d \' -f 4`
   WPDBPASS=`cat /home/$domain/public_html/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
   
   echo "$domain is WordPress with database: $WPDBNAME"
   
   read -r -p "Do you want to backup $domain? [y/N] " response
   case $response in [yY][eE][sS]|[yY])
    echo "- Backup database: $WPDBNAME"
    mysqldump -u $WPDBUSER -p$WPDBPASS $WPDBNAME > /home/$domain/public_html/database-$(date +"%Y-%m-%d").sql
    echo "- Backup files: $domain"
    zip -r $backup_path/$domain-$(date +"%Y-%m-%d").zip /home/$domain/public_html/ -q -x /home/$domain/public_html/wp-content/cache/**\* #Exclude cache
    rm -rf /home/$domain/public_html/database-$(date +"%Y-%m-%d").sql #Remove database file
    echo "- Done, database and files are saved to $backup_path/$domain-$(date +"%Y-%m-%d").zip"
    echo "- Download link: http://$main_site:$port/backupallwp/$domain-$(date +"%Y-%m-%d").zip or http://$serverip:$port/backupallwp/$domain-$(date +"%Y-%m-%d").zip"
    ;;
       *)
           echo "Okay, don't backup"
           ;;
   esac
   
  else
   echo "$domain is not WordPress"
  fi
  
 fi
done

nguồn: http://hocvps.com/hocvps-script-plugin-backup-all-wordpress-sites/

0 comments:

Post a Comment