#!/bin/bash
# wordpress-auto-backup.sh
### THIS SCRIPT ASSUMES THE FOLLOWING
# 1/ You can do SSH password-less login to the server
# How : https://easyengine.io/tutorials/linux/passwordless-authentication-ssh/
# 2/ You have created a correct ~/.my.cnf file
# How : https://easyengine.io/tutorials/mysql/mycnf-preference/
### RUNTIME PARAMETERS
# wordpress-auto-backup.sh <server address> <home directory> <backup directory>
### HOW IT WORKS
# 1/ SSH to server
# 2/ read wp-config file & get details
# 3/ create backup_dir/web
# 4/ rsync home_dir backup_dir/web
# 5/ backup database to backup_dir/web/today.mysql
# 6/ compress backup_dir/web to backup_dir/backup_today.bz2
### choose backup retention
# backup retention: weekly
today=`date +%A`
# backup retention: monthly
#today=`date +%d`
timestamp=`date +%H-%M-%S`
compressor1='gzip'
compressor2='pbzip2'
# to help making this code more readable
server=$1
home_dir=$2
backup_dir=$3
ssh_user=$4
website=$5
#============= START BACKUP =======================
# get the variables
ssh $ssh_user@$server "cat $home_dir/wp-config.php" > /tmp/$server$today$timestamp.txt
db_name=`cat /tmp/$server$today.txt | grep DB_NAME | cut -d"'" -f 4`
db_user=`cat /tmp/$server$today.txt | grep DB_USER | cut -d"'" -f 4`
db_pass=`cat /tmp/$server$today.txt | grep DB_PASSWORD | cut -d"'" -f 4`
db_host=`cat /tmp/$server$today.txt | grep DB_HOST | cut -d"'" -f 4`
table_prefix=''
table_prefix=`cat /tmp/$server$today$timestamp.txt | grep table_prefix | cut -d"'" -f 2`
# debug
#echo $db_name $db_user $db_pass $db_host $table_prefix
# delete temporary file
rm /tmp/$server$today$timestamp.txt
# backup database
ssh $ssh_user@$server "mysqldump -h $db_host -u $db_user --no-tablespaces --password=\"$db_pass\" $db_name \$(mysql -h $db_host -u $db_user --password=\"$db_pass\" -D $db_name -Bse \"show tables like '$table_prefix%'\") > $home_dir/db-$today.mysql"
# compress database dump
ssh $ssh_user@$server "$compressor1 -f $home_dir/db-$today.mysql"
# download everything
mkdir -p $backup_dir/web/
rsync -avuz $ssh_user@$server:$home_dir/* $backup_dir/web/
# make backup file
tar cvf $backup_dir/$website-$today.tar $backup_dir/web/
# remote clean up
ssh $ssh_user@$server "rm $home_dir/db-$today.mysql.gz" &
# compress backup
$compressor2 -f $backup_dir/$website-$today.tar
# local clean up
rm $backup_dir/web/db-$today.mysql.gz
### check
#ls -l $backup_dir/web
#ls -l $backup_dir
# done !
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|0 👍
/0 👎
0 B
|👍
/👎
0 B
|👍
/👎