There’re tons of WordPress backup scripts in GitHub and other places, but most of them are too complicated for my blog site. Luckily I found a quite simple but very useful one here.

The simplest backup script I found

Based on the script above, I adjusted it to fit my own needs.

#!/bin/bash

#### Settings ####
NOW=$(date +"%Y-%m-%d-%H%M")
#### Site-specific Info ####
SITE_PATH="/var/www/html/cwyyprog" #Could also be subsites/subsitename
DB_NAME=`cat $SITE_PATH/wp-config.php | grep DB_NAME | cut -d \' -f 4`
DB_USER=`cat $SITE_PATH/wp-config.php | grep DB_USER | cut -d \' -f 4`
DB_PASS=`cat $SITE_PATH/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
DB_HOST=`cat $SITE_PATH/wp-config.php | grep DB_HOST | cut -d \' -f 4`
#zip -r $SITE_PATH.$NOW.zip $SITE_PATH
mysqldump -h $DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME > /tmp/$DB_NAME.$NOW.sql
tar czf /tmp/wordpressBakup.tar.gz /tmp/$DB_NAME.$NOW.sql $SITE_PATH /etc/nginx/sites-available/cwyyprog
#change 172.16.4.124 to your backup host server's ip
#`date -Idate | cut -d- -f3` % 4 keep only latest 4 backups
scp  -i wordpressBackup.id_rsa  /tmp/wordpressBakup.tar.gz [email protected]:/home/qx/wordpressBakup_$((`date -Idate | cut -d- -f3` % 4)).tar.gz
rm /tmp/$DB_NAME.$NOW.sql /tmp/wordpressBakup.tar.gz

I suggest you create an unique restricted user on your backup server and generate a separate SSH key for this backup script. The steps to generate a SSH key and copy to backup server are as follows:

root@ecs-1yzaB:~# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): wordpressBackup.id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in wordpressBackup.id_rsa.
Your public key has been saved in wordpressBackup.id_rsa.pub.

ssh-copy-id -i wordpressBackup.id_rsa.pub [email protected]

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据