WordPress Auto-Backup via SSH

This script will enable you to backup your WordPress websites automatically. Just put it in a crontab / automatic scheduling software somewhere.

Also available on Pastebin : https://pastebin.com/nZ2fiL8j

Enjoy.

#!/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`

compressor1='gzip'
compressor2='pbzip2'

#============= START BACKUP =======================

# to help making this code more readable
server=$1
home_dir=$2
backup_dir=$3

# get the variables
ssh $server "cat $home_dir/wp-config.php" > /tmp/$server$today.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.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.txt

# backup database
ssh $server "mysqldump -h $db_host -u $db_user --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 $server "$compressor1 $home_dir/db-$today.mysql"

# download everything 
mkdir $backup_dir/web/
rsync -avuz $server:$home_dir/* $backup_dir/web/

# make backup file
tar cvf $backup_dir/$server-$today.tar $backup_dir/web/

$compressor2 $backup_dir/$server-$today.tar &

# clean up
ssh $server "rm $home_dir/db-$today.mysql.gz"

# done !


78 thoughts on “WordPress Auto-Backup via SSH

  1. untuk implementasinya apakah akan sulit mas? saya kurang tahu dan paham cara penerapannya gimana supaya bisa backup via SSH ini..

  2. Hello would you mind sharing which blog platform you’re working with? I’m looking to start my own blog in the near future but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S Apologies for getting off-topic but I had to ask!

  3. Segera daftar di situs judi online terbaik BK8. Di website BK8 anda diberikan layanan livestreaming bola tanpa delay jika sudah menjadi member.

  4. “Adeena’s Kitchen is a Restaurant and Takeaway in Croydon. We serve popular and authentic Indian Cuisine to customers from all over the world.Adeena’s Kitchen is a Restaurant and Takeaway in Croydon. We serve popular and authentic Indian Cuisine to customers from all over the world.

    The chef has 30 years of experience especially in Chicken and Mutton Curry, Biryani Dishes and Seafood – all with special herbs and ingredients of Indian cuisine.

  5. To the sufehmi.com webmaster, You always provide great examples and real-world applications, thank you for your valuable contributions.

  6. royal5555 ???? ????? ????????????????????????????????????????????????????????????????? ??????????????????????????? pg slot ?????????????????????????????????????????????????????

  7. pg lion99 slot games is an online casino platform that is rapidly growing in the online gambling game industry. With a wide variety of exciting games and quality service, pg slot this website is what you expect.

  8. Slot 5000 Adalah Pilihan Terbaik Para Bettor Online Bermain Di Situs Slot Gacor Mudah Maxwin Dengan Menggunakan Server Slot Thailand Yang Slalu Memberikan Kemenangan Besar Untuk Player Setia. Daftar Dan Rasakan Sensationalnya Disini.

  9. PokerV ataupun Poker V, adalah server terbaik yang menawarkan permainan kartu QQ seperti DominoQ, BandarQ, dan AduQ. Sebagai agen judi online terpercaya di Indonesia, PokerV menyediakan pilihan deposit yang selalu online 24 jam aktif. Bergabunglah dengan kami di situs PokerV online dan nikmati pengalaman bermain kartu yang luar biasa!

  10. Selamat Datang Di PKV Games Resmi Dan Terpercaya Indonesia. Server Yang Menyediakan Kumpulan Permainan QQ Seperti DominoQQ, BandarQQ & AduQ Dengan Winrate Tertinggi

  11. slotxo ???????????????????????? ?????? ??????????? ???????????????? ???????????????????????????? ????????????????????? ???????????????????????? ??????????????????????????????????????? ??????? ????????????????? ????????????? ???????????? ??? ? ?????????????????????????? ????????????????????????????????????? ????????? ??????????????????????? ?????????????????? ?????????????????????? ?????????????????????????????????

  12. I do not even know how I ended up here but I thought this post was great I do not know who you are but certainly youre going to a famous blogger if you are not already Cheers

  13. Hi i think that i saw you visited my web site thus i came to Return the favore I am attempting to find things to improve my web siteI suppose its ok to use some of your ideas

  14. I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts

  15. Thank you for sharing your personal experiences and stories It takes courage to open up and you do it with such grace and authenticity

Leave a Reply

Your email address will not be published. Required fields are marked *