0 Replies - 1102 Views - Last Post: 04 January 2008 - 08:55 PM

#1 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 181
  • View blog
  • Posts: 2,642
  • Joined: 30-December 07

Simple Bash backup script

Posted 04 January 2008 - 08:55 PM

Description: Configuration consists of editing a few variables within the script (see "Usage" in the header of the script). As with any Bash script, run it with the "sh" command.A simple backup script written in Bash. Generates a folder with the day's date, and fills it with .tar.bz2 archives of whatever folders you specify.
#!/usr/bin/bash
#
# gen-backups.sh - A simple backup script.
#
# Author: Tom Arnold, 2008.
#
# Description: This script isn't very "smart",
#              it'll backup the same old data
#              over and over. If you want "smart"
#              backups, run a CVS server (or write
#              your own script).
#
#              The point of this script is to offer
#              an alternative to backing up your whole
#              home directory, or doing everything
#              manually.
#
# Usage: Edit the below variables, then run it.
#        Each specified directory will have a
#        ".tar.bz2" archive in $BACDIR/$SUBDIR.
#
# License: Public domain.
#
##

# Directory to store backups in.
BACDIR="/home/tom/backup"

# Name to use for the backup subdirectories.
SUBDIR=backup-$(date +%m%d%y)

# Directories to be backed up.
#
# You can add as many of these as you want,
# just make sure the index starts at zero
# and increments by one.
DIRS[0]="/home/tom/workspace"
DIRS[1]="/home/tom/school"
DIRS[2]="/home/tom/themes"
DIRS[3]="/home/tom/scripts"
DIRS[4]="/home/tom/Desktop"


# Don't edit anything below this line!


NUMDIRS=${#DIRS[*]}

echo "Running backups script..."
cd "$BACDIR"
cd ..

# If the backup dir doesn't exist, create it.
if [ ! -d "$BACDIR" ]; 
then 
    mkdir "$BACDIR" 
fi

cd "$BACDIR"

# If the directory we want to make already
# exists, keep adding numbers into the name
# until we get a directory name that doesn't.
if [ -d "$SUBDIR" ];
then
    I=0

    while [ -d "$SUBDIR""_"$I ]
    do
	let I=$I+1
    done
    
    SUBDIR+="_"$I
fi

mkdir "$SUBDIR"

# Loop through and make backups.
I=0

while [ $I -lt $NUMDIRS ]
do
    # Check if the dir exists...
    if [ -d "${DIRS[$I]}" ];
    then
	# Make a name that's a little more friendly.
	MAGIC=".190427150621502"

	echo "${DIRS[$I]}" > $MAGIC
	NAME="backup"$(sed -e 's///-/g' $MAGIC )
	rm $MAGIC

	tar cjf "$BACDIR/$SUBDIR/$NAME".tar.bz2 "${DIRS[$I]}"
	echo "Backed up ($[$I+1]/$NUMDIRS) directories."
    else
	echo "Error, directory "${DIRS[$I]}" does not exist or is not a directory."
    fi

    let I=$I+1
done

echo "Backups complete, have a nice day!"



Is This A Good Question/Topic? 0
  • +

Page 1 of 1