Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Backup Script for VPS systems
#1
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.
PGP Key (Fingerprint: 23B6 F4C0 FE2D 45AA 61A0 1E86 DB87 09DC DD87 6E40)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)