zigford
October 23rd, 2005, 12:46 AM
Hey, I have been looking for a good way to backup my Ubuntu system for ages. There seem to be no good programs in our repositories or I am blind.
So I decided to write a beauifull little script. It started out as a simple task and just kept getting bigger and better. It was based off of various scripts I found on the Google.
Features:
Will scan home directorys and exclude files larger than 10meg (You can change this variable)
Reads ".exclude" files from home directories for manual exclusions.
Reads ".exclude" files from any of the other specified inclusions for exclusion.
Creates archives with date tags
Easily editable variables
Verifies backup integrity
#!/bin/bash
# Backup Script by Jesse Harris
# Create backups of /etc, /home, /usr/local, and...
PATH=/bin:/usr/bin
################################################## #####################
# Please edit these variables to match your system #
# #
# backupdirs - This specifies seperate archives you wish to create #
# - You can put a .exclude file into these directories #
# - for manual exclusions (Specify the full path) #
# dir - This is where you backups will be stored, if it is #
# - within your inclusion path, make sure to put it into #
# - its relevant .exclude file. #
# homes - This is a temp file created whenever the script is run #
# sizecheck - This specifies that home directories are to be scanned #
# - for file sizes, do not change this variable #
# maxsize - This specifies that anything exceeding this size within#
# - anyones home directory will be automatically excluded #
#-----------------------------Good Luck-------------------------------#
################################################## #####################
backupdirs="/etc /root /home /boot /var /bin /initrd /lib /sbin /usr"
dm=`date +%d%b`
dir="/home/harrisj/backup/"
homes="/tmp/homes"
sizecheck="/home"
maxsize="+10M"
# Check if root
if [ "$(whoami)" != "root" ]; then
echo "Not running as root. Exiting..."
exit 0
else
echo "Running as root. Good"
fi
mkdir -p $dir$dm
for path in $backupdirs
do
if [ "$path" = "$sizecheck" ]; then
ls $path > $homes
for home in $(cat $homes)
do
if [ "$home" != "$sizecheck" ]; then
echo "Checking $home Filesizes..."
find $path/$home/ -size $maxsize -type f -print > $path/$home/auto-exclude
echo "System backup on $path/$home"
tar --exclude-from=$path/$home/auto-exclude --exclude-from=$path/$home/.exclude -czf $dir$dm/$home.tar.gz $path/$home 2>/dev/null
fi
done
else
if [ -e $path/.exclude ]; then
echo "Reading exclude file.. for $path"
echo "System backup on $path"
tar --exclude-from=$path/.exclude -czf $dir$dm/$path.tar.gz $path 2>>/dev/null
else
echo "System backup on $path"
tar czf $dir$dm/$path.tar.gz $path 2>>/dev/null
fi
fi
sleep 2
done
echo "System backups complete, status: $?"
echo "Now verifying system backups"
for path in $backupdirs
do
echo "Verifying $path...."
if [ "$path" = "/home" ]; then
for home in $(cat $homes)
do
zcat $dir$dm/$home.tar.gz | tar t 2>/dev/null && \
if [ $? -eq 0 ]
then echo "$path: verified"
else echo "$path: error(s) in verify" | wall
fi
done
else
zcat $dir$dm/$path.tar.gz | tar t 2>/dev/null && \
if [ $? -eq 0 ]
then echo "$path: verified"
else echo "$path: error(s) in verify" | wall
fi
fi
done
echo "Please remove backup tape" | wall
Example .exclude file from /home/harrisj/.exclude
Always specify the full path in your .exclude files
/home/harrisj/backup/*
/home/harrisj/c/*
/home/harrisj/dvdrip/*
/home/harrisj/Qemu/*
/home/harrisj/iso/*
/home/harrisj/Games/*
/home/harrisj/Temp/*
/home/harrisj/Video/*
/home/harrisj/vcr/*
/home/harrisj/.Trash
/home/harrisj/.transgaming/*
~/music
~/roms
~/winetools
Also, its handy to have a nautilus script to easily add directories to your home .exclude file
vi ~/.gnome2/nautilus-scripts/Add\ To\ Backup\ Exclude
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
dir="$base"
else
while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
dir="$base/$1"
fi
echo $dir >> ~/.exclude
chmod ug+x ~/.gnome2/nautilus-scripts/Add\ To\ Backup\ Exclude
Now just right click on the directory-->Scripts-->Add to backup exclude.
Good bye
Edit: Bugfix
Edit: Script update
Edit: missed a slash in there.
Edit: Changed a line of code that was wrong.
So I decided to write a beauifull little script. It started out as a simple task and just kept getting bigger and better. It was based off of various scripts I found on the Google.
Features:
Will scan home directorys and exclude files larger than 10meg (You can change this variable)
Reads ".exclude" files from home directories for manual exclusions.
Reads ".exclude" files from any of the other specified inclusions for exclusion.
Creates archives with date tags
Easily editable variables
Verifies backup integrity
#!/bin/bash
# Backup Script by Jesse Harris
# Create backups of /etc, /home, /usr/local, and...
PATH=/bin:/usr/bin
################################################## #####################
# Please edit these variables to match your system #
# #
# backupdirs - This specifies seperate archives you wish to create #
# - You can put a .exclude file into these directories #
# - for manual exclusions (Specify the full path) #
# dir - This is where you backups will be stored, if it is #
# - within your inclusion path, make sure to put it into #
# - its relevant .exclude file. #
# homes - This is a temp file created whenever the script is run #
# sizecheck - This specifies that home directories are to be scanned #
# - for file sizes, do not change this variable #
# maxsize - This specifies that anything exceeding this size within#
# - anyones home directory will be automatically excluded #
#-----------------------------Good Luck-------------------------------#
################################################## #####################
backupdirs="/etc /root /home /boot /var /bin /initrd /lib /sbin /usr"
dm=`date +%d%b`
dir="/home/harrisj/backup/"
homes="/tmp/homes"
sizecheck="/home"
maxsize="+10M"
# Check if root
if [ "$(whoami)" != "root" ]; then
echo "Not running as root. Exiting..."
exit 0
else
echo "Running as root. Good"
fi
mkdir -p $dir$dm
for path in $backupdirs
do
if [ "$path" = "$sizecheck" ]; then
ls $path > $homes
for home in $(cat $homes)
do
if [ "$home" != "$sizecheck" ]; then
echo "Checking $home Filesizes..."
find $path/$home/ -size $maxsize -type f -print > $path/$home/auto-exclude
echo "System backup on $path/$home"
tar --exclude-from=$path/$home/auto-exclude --exclude-from=$path/$home/.exclude -czf $dir$dm/$home.tar.gz $path/$home 2>/dev/null
fi
done
else
if [ -e $path/.exclude ]; then
echo "Reading exclude file.. for $path"
echo "System backup on $path"
tar --exclude-from=$path/.exclude -czf $dir$dm/$path.tar.gz $path 2>>/dev/null
else
echo "System backup on $path"
tar czf $dir$dm/$path.tar.gz $path 2>>/dev/null
fi
fi
sleep 2
done
echo "System backups complete, status: $?"
echo "Now verifying system backups"
for path in $backupdirs
do
echo "Verifying $path...."
if [ "$path" = "/home" ]; then
for home in $(cat $homes)
do
zcat $dir$dm/$home.tar.gz | tar t 2>/dev/null && \
if [ $? -eq 0 ]
then echo "$path: verified"
else echo "$path: error(s) in verify" | wall
fi
done
else
zcat $dir$dm/$path.tar.gz | tar t 2>/dev/null && \
if [ $? -eq 0 ]
then echo "$path: verified"
else echo "$path: error(s) in verify" | wall
fi
fi
done
echo "Please remove backup tape" | wall
Example .exclude file from /home/harrisj/.exclude
Always specify the full path in your .exclude files
/home/harrisj/backup/*
/home/harrisj/c/*
/home/harrisj/dvdrip/*
/home/harrisj/Qemu/*
/home/harrisj/iso/*
/home/harrisj/Games/*
/home/harrisj/Temp/*
/home/harrisj/Video/*
/home/harrisj/vcr/*
/home/harrisj/.Trash
/home/harrisj/.transgaming/*
~/music
~/roms
~/winetools
Also, its handy to have a nautilus script to easily add directories to your home .exclude file
vi ~/.gnome2/nautilus-scripts/Add\ To\ Backup\ Exclude
base="`echo $NAUTILUS_SCRIPT_CURRENT_URI | cut -d'/' -f3- | sed 's/%20/ /g'`"
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then
dir="$base"
else
while [ ! -z "$1" -a ! -d "$base/$1" ]; do shift; done
dir="$base/$1"
fi
echo $dir >> ~/.exclude
chmod ug+x ~/.gnome2/nautilus-scripts/Add\ To\ Backup\ Exclude
Now just right click on the directory-->Scripts-->Add to backup exclude.
Good bye
Edit: Bugfix
Edit: Script update
Edit: missed a slash in there.
Edit: Changed a line of code that was wrong.