Linux BASH Backup to FTP Script

The other day  I was trying to figure out how I can automate my backup process on my Ubuntu boxes, so I ventured out into Google and started searching for some scripts that someone else with the same goals might have written.  I found  a few different scripts ; however, none of them did what I wanted them to do, or they did not work. This was my first attempt at making a BASH script.  As a kid I used to make DOS batch files all day, doing all sorts of silly things, so making this script brought back some good memories.

This script backs up certain directories in your /home/user folder. In my script, I only have it backing up my /home/ron/Desktop and /home/ron/Documents, this those are the two directories I use the most.  As you see below, the script compresses the folders in a nice tar.gz file, date stamps the file, then uploads it to a remote FTP.

Here it is


#!/bin/bash
#  Individual Directory Backup Script

#  Created December 23, 2009

#  Last edited December 23, 2009
#  Written by: Ron Messana
#
# Backup TimeStamp
BUTDSTAMP=$(date +%Y%m%d)
# System Identifier
SYSTEM="ron-laptop"
# Directories to Backup // MAKE SURE YOU CHANGE THE MPUT AND TAR.GZ LINE WHEN ADDING/REMOVING DIRECTORIES TO BE BACKED UP
DIR1="/home/ron/Desktop"
DIR1NICK="desktop"
DIR2="/home/ron/Documents"
DIR2NICK="documents"
DIR3=""
DIR3NICK=""
DIR4=""
DIR4NICK=""
DIR5=""
DIR5NICK=""
# ftp credentials
USERNAME="username"
PASSWORD="password"
# ftp server hostname
SERVER="ftp.server.com"
# local directory to pickup *.tar.gz file
FILE="/home/ron/backup"
# remote server directory to upload backup
BACKUPDIR="/bu"
clear
echo "            DIRECTORY BACKUP SCRIPT STARTING"
echo "                BACKING UP $DIR1"
echo "                BACKING UP $DIR2"
echo ""
echo ""
echo ""
echo ::
# Compressing directories as a tar gzip file // CHANGE LINES TO CORRESPONG WITH THE AMOUNT OF DIRECTORIES BEING BACKED UP
tar -pzcvf $SYSTEM-$DIR1NICK-$BUTDSTAMP.tar.gz $DIR1
tar -pzcvf $SYSTEM-$DIR2NICK-$BUTDSTAMP.tar.gz $DIR2
ls *.tar.gz
mv *.tar.gz /home/ron/backup
#
# login to remote server // CHANGE MPUT LINE(s) TO CORRESPOND WITH THE AMOUNT OF DIRECTORIES BEING BACKED UP
echo ""
echo ""
echo "Uploading to $SERVER via FTP"
echo "Please wait, this may take some time, depending on your connection speed"
echo ""
ftp -n -i $SERVER <<EOF
user $USERNAME $PASSWORD
cd $BACKUPDIR
lcd $FILE
mput $SYSTEM-$DIR1NICK-$BUTDSTAMP.tar.gz
mput $SYSTEM-$DIR2NICK-$BUTDSTAMP.tar.gz
quit
EOF
echo ""
echo " Removing local archives"
echo ""
rm /home/ron/backup/*.tar.gz
echo " Finished"
#
#
#

Linux Backup Script

  • Share/Bookmark

1 Comment »

  1. Shaniqua Said,

    January 11, 2010 @ 4:48 pm

    very helpful, thanks!

Leave a Comment