MyBB Community Forums
Backup Script for VPS systems - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Resources (https://community.mybb.com/forum-8.html)
+--- Forum: Tutorials (https://community.mybb.com/forum-38.html)
+--- Thread: Backup Script for VPS systems (/thread-127720.html)



Backup Script for VPS systems - Josh H. - 2012-10-16

Hey everyone,
I just wanted to share a script I found at WHT and modified to add in a DB backup and delete any same-name file to avoid errors. I'm pretty happy with what I have done, quite honestly. It takes about 20 seconds to run on my VPS (miniature 4MB database and about 80MB of files in home dir). After downloading over SFTP, I have a backup within about a minute and a half.

WHT Thread Link: http://www.webhostingtalk.com/showthread.php?t=1130207
Quote:#!/bin/sh
####################################
#
# Create archive of files in home dir and database
#
####################################

# What to backup.
backup_files="/home/username/"

# Where to backup to.
dest="/home/username"

# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
echo "Removing $dest/$archive_file"
rm -rf $dest/$archive_file

echo "#####################################################"
echo "# Database Backup #"
echo "####################################################"
sleep 1
echo ".................................................................................................................................................................................................................................."
echo ".................................................................................................................................................................................................................................."
sleep 1
mysqldump -u root -pMYSQLUSERPASSWORD dbname > /home/username/filename.sql
sleep 1
echo
echo "..."
echo "Completed"
# Print start status message.
echo
echo
echo "####################################################"
echo "# Archive Building #"
echo "####################################################"
sleep 1
echo ".................................................................................................................................................................................................................................."
echo ".................................................................................................................................................................................................................................."
sleep 1

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Removing database dump for security"
sleep 1
rm -rf /home/username/file.sql
sleep 1
echo "..."
echo "Backup finished"
For what it's worth, I added the sleep's just for my personal satisfaction. I didn't like the instant processing, so I wanted a gap in between each message.

This can be executed by running
Quote:bash ./path/to/file.sh

Alternatively, I added the following to my .bashrc profile
Quote:alias bkp="bash /path/to/file.sh"

I am guessing if you are on shared hosting, you can probably execute this file with a few edits, of course.