Tag Archives: backup

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 !


Memasukkan Data CSV ke MySQL

:: Beberapa hari yang lalu, saya diminta oleh seorang data analyst untuk memasukkan sekumpulan data di file CSV ke MySQL. Alhamdulillah ini mudah dilakukan di MySQL, dengan memanfaatkan perintah "LOAD DATA INFILE"

Isi dari file CSV tersebut adalah seperti ini :

======
NAMA; ALAMAT; TELPON; KODE POS
Saya; Jl. Jalan Disana; 0215554848; 12345
Kamu; Jl. Jalan Disini; 0215558348; 98765
======

Maka, perintah untuk memasukkannya ke sebuah table di MySQL dengan format fields yang sama adalah sebagai berikut : 

======
mysql -e "LOAD DATA INFILE '/tmp/tmp/inidata.csv' INTO TABLE datasaya.tablesaya FIELDS TERMINATED BY ';' LINES TERMINATED BY 'rn' "
======

Keterangan :

FIELDS TERMINATED BY ';' : karena setiap field di file CSV tersebut ternyata dipisahkan oleh ';', bukan koma.
LINES TERMINATED BY 'rn' : tanpa opsi ini, maka ketika data tersebut di SELECT, outputnya akan berantakan.

BERSIH-BERSIH

Pembaca yang bermata & bernalar tajam tentu akan langsung paham, bahwa, perintah tersebut diatas juga akan memasukkan record pertama 🙂 yaitu : 

======
NAMA; ALAMAT; TELPON; KODE POS
======

Untunglah solusinya mudah, yaitu perintah berikut ini :

======
mysql -e "DELETE FROM tablesaya WHERE kode_pos='KODE POS';"
======

BANYAK FILE

Happy ending ? Belum 🙂 karena, ternyata datanya ada ratusan ribu record, DAN tersebar di ratusan file, di puluhan sub-direktori…. #glek 

Untunglah ini dunia Unix / Linux 🙂 sehingga, solusinya cukup berupa script ringkas sbb :

=============
#!/bin/bash
DATABASE=datasaya
TABLE=tablesaya

#backup current table
mysqldump $DATABASE $TABLE > ~/backup/$DATABASE-$TABLE—`date +%d-%m-%Y`.mysql

# set line terminator to be n 
# (default: space)
# if this is not set, then $FILES content will be really messed up 
# when there are files with space in their name
IFS=$'n';

FILES="$(find /tmp/tmp -type f -name '*')"

for f in $FILES
do
  echo "Processing $f file…"

mysql -e "LOAD DATA INFILE '$f' INTO TABLE $DATABASE.$TABLE FIELDS TERMINATED BY ';' LINES TERMINATED BY 'rn' "
mysql -e "DELETE FROM $TABLE WHERE kode_pos='KODE POS';"

done
=============

Semoga bermanfaat.

Post imported by Google+Blog for WordPress.