sh 从命令行创建wordpress备份。专为cPanel文件结构而设计

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 从命令行创建wordpress备份。专为cPanel文件结构而设计相关的知识,希望对你有一定的参考价值。

#!/bin/bash

# Author: Milo Jennings
# Date: 03/20/2016
# Version: 1.02
# 
# 
# This script will create compressed backup archive of a given wordpress install,
# along with it's database.
# 
# Besides performing a backup, the script also optimizes the database. Dumps logs from the
# redirection plugin, and deletes all unapproved comments.
# 
# This is designed specifically for the file structure of a cPanel server.
# Backups are stored like so %home_folder%/backups/%domainname.com% - %timestamp%.tar.gz
# 
# Usage:
# ------------------------------------------------------------------------------------------
# Specify the user folder, and it will take care of the rest.
# $ wordpress-backup-script.sh foldername
# 
# 
# Changelog:
# ------------------------------------------------------------------------------------------
# 1.02
# - Excluding backupbuddy backups
# - Fixed filename output
# - Supressed output from tar command
# - Added check for database. DB skipped if wp-config isn't found.
# 
# # 1.01
# - Permission fix, if running as root
# 
# 1.0 
# - Release

if [[ -z "$1" ]]; then
	echo "You must specify a home folder"
	exit
else
	USER_FOLDER=$1
	if [[ -d /home/$USER_FOLDER/public_html ]] 
	then  
	  `# "Directory exists"`
	else
	  echo "Directory (/home/$USER_FOLDER/public_html) does not exist"
	  exit
	fi
fi

# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
NOW=$(date +"%Y-%m-%d-%H%M")

BACKUP_DIR="/home/$USER_FOLDER/backups"
if [[ -d $BACKUP_DIR ]]; then
	`# Backup director exists`
else
	echo "Backup directory doesn't exist. Creating ($BACKUP_DIR)"
	mkdir $BACKUP_DIR
fi

WWW_DIR="/home/$USER_FOLDER/public_html/"

if [[ -f "${WWW_DIR}wp-config.php" ]]; then
	DATABASE_EXISTS=true

	# MySQL database credentials
	DB_USER=`grep "DB_USER" ${WWW_DIR}wp-config.php | cut -d \' -f 4`
	DB_PASS=`grep "DB_PASSWORD" ${WWW_DIR}wp-config.php | cut -d \' -f 4`
	DB_NAME=`grep "DB_NAME" ${WWW_DIR}wp-config.php | cut -d \' -f 4`
	DB_PREFIX=`grep "table_prefix" ${WWW_DIR}wp-config.php | cut -d \' -f 2`

	DB_FILE="$DB_NAME.$NOW.sql"

	## Get the site's url from the database
	DB_TABLE="${DB_PREFIX}options"
	SITE_DOMAIN=$(mysql -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -se "SELECT * FROM $DB_TABLE WHERE option_name LIKE 'siteurl'" \
	| cut -f 3 `#Isolate the URL column from the database query http://www.example.com` \
	| sed s/'http:\/\/'/''/g `#Remove the http:// prefix` \
	| sed s/'https:\/\/'/''/g `#Remove the https:// prefix` \
	| sed "s/^www.//" `#Remove www. prefix`)
	
	BACKUP_FILE="$SITE_DOMAIN - $NOW.tar"
else
	# echo "wp-config.php NOT found"
	DATABASE_EXISTS=false
	BACKUP_FILE="$USER_FOLDER - $NOW.tar"
fi

# Tar transforms for better archive structure.
WWW_TRANSFORM="s,^home/$USER_FOLDER/public_html,public_html,"

# Archive WP files
tar --exclude=backupbuddy_backups -cf "$BACKUP_DIR/$BACKUP_FILE" --transform $WWW_TRANSFORM $WWW_DIR


if ($DATABASE_EXISTS); then
	DB_TRANSFORM="s,^home/$USER_FOLDER/backups,db,"
	mysql -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DROP TABLE IF EXISTS ${DB_PREFIX}redirection"
	mysql -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DROP TABLE IF EXISTS ${DB_PREFIX}redirection_404"
	mysql -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DROP TABLE IF EXISTS ${DB_PREFIX}redirection_logs"
	echo "Redirection plugin tables dropped..."

	mysql -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DELETE FROM ${DB_PREFIX}comments WHERE comment_approved = 0"
	echo "Unapproved comments deleted..."

	mysqlcheck -s --optimize "$DB_NAME" --user="$DB_USER" --password="$DB_PASS"
	echo "Database optimized..."

	# Create MySQL dump
	mysqldump -h "localhost" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" > $BACKUP_DIR/$DB_FILE
	echo "Database backed up..."

	# Append the databse dump to the archive, 
	tar --append --file="$BACKUP_DIR/$BACKUP_FILE" --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE > /dev/null

	# Remove the dump
	rm "$BACKUP_DIR/$DB_FILE"
fi

# Compress the whole archive.
gzip -9 "$BACKUP_DIR/$BACKUP_FILE"


USER_PERM=`stat -c "%U" /home/$USER_FOLDER/public_html`
chown -Rf $USER_PERM:$USER_PERM $BACKUP_DIR
echo "Permissions fixed"

echo "------"
echo "Backup complete"
echo "------"
echo "Archive saved to:"
echo "${BACKUP_DIR}/${BACKUP_FILE}.gz"

以上是关于sh 从命令行创建wordpress备份。专为cPanel文件结构而设计的主要内容,如果未能解决你的问题,请参考以下文章

cp 花括号备份{}

sh 通过命令行安装wordpress

通过命令行使用“复制”和“粘贴”时备份文件

LINUX CP命令

sh Wordpress备份和焦油

sh 从命令行创建一个新的Bitbucket回购