Hosting Tutorial & Guide

How to Automate VPS Backup For Free?

updated on Sep 14, 2014
VPS Backup
Most VPS don't provide automated website backup for free, but the daily or weekly backup of your website is important, because VPS is not reliable and scalable as dedicated server, in fact. This article is written to share with you the simple shell scripts and approach, which are used for backing up our websites developed based on PHP and MySQL, without additional cost.

The prerequisite of the approach is that you shall have a free web space which supports FTP. The mechanism is, you follow this article to create the shell scripts and schedule the execution by Cron Jobs. The shell scripts back up the MySQL databases and website files, and compress them to a ZIP file, then upload to your web space via FTP.

Let's first look at the Shell scripts to back up the MySQL database.
#set -x
time=`date +%Y%m%d`
#Back up MySql
[ ! -e /backup/sqltmp ] && mkdir -p /backup/sqltmp #Create a temporary folder for MySQL backup
[ ! -e /backup/backsql ] && mkdir -p /backup/backsql #Create a final folder for MySQL backup
cd /backup/sqltmp

#Query custom database in MySQL
sql=`mysqlshow -u[UserName] -p[Password] |grep -v + | grep -Ev “Data|information|mysql|test” | awk {‘print $2′}` 
for sqlname in $sql
mysqldump -u[UserName] -p[Password] $sqlname > $sqlname.sql
tar zcvf backsql.$time.tar.gz *.sql
rm -rf *.sql
mv * ../backsql/.
cd ..
rm -rf sqltmp
#Back up website
[ ! -e /backup/webtmp ] && mkdir -p /backup/webtmp #Create a temporary folder for website backup
[ ! -e /backup/backweb ] && mkdir -p /backup/backweb #Create a final folder for website backup
cd /backup/webtmp
tar zcvf backweb.$time.tar.gz /home/*
mv * ../backweb/.
cd ..
rm -rf webtmp

Then, create the Shell scripts to upload the backup from your VPS to the public web space.
backsql=`cd /backup/backsql;ls -r backsql* | head -1` #Get the latest backup
backweb=`cd /backup/backweb;ls -r backweb* | head -1`
ftp -n<<EOF
open ip #The IP or URL of the FTP server
user ftp[UserName] ftp[Password]
cd /back/ #FTP path for storing the backup
lcd /backup/backsql/ #Path of backup on the VPS
mput $backsql
lcd /backup/backweb/ #Path of backup on the VPS
mput $backweb

Finally, upload the script files to your VPS, and create a Cron Job for the execution. Now, you get the automated backup, making your online business more secure and reliable, without cost.

Best VPS Hosting

Best VPS Hosting MyHosting VPS Hosting
  • Pricing from $11.96/month, using coupon code VPS123
  • Fully customizable on CPU, memory, disk space
  • Data center in both USA and Canada
  • Full control with root access via SSH
The competitive power of MyHosting VPS hosting not only comes from its affordability but its flexibility, reliability and scalability. The entry-level of VPS starts from $11.96/month only if you're using coupon code VPS123. It provides 1GHZ CPU, 512MB memory, 20GB hard disk, 300GB bandwidth and 1 dedicated IP address. This is complete enough for a website with traffic under 5000 page views daily, if it’s optimized well.