Skip to content

the thinkpod

where I think out loud

Menu
  • Timeline
Menu

Backup Your Linux Machine

Posted on October 16, 2012 by milad

I was up to maintenance of my Debian box, and I came in need of creating and maintaining multiple, date-based backups of the machine.

Here is the bash script I wrote to accomplish this.

#!/bin/bash
#Shell Script to backup desired target

verbose="0"
tarv=""

if test "$1" == "-v";
then
	verbose="1"
	tarv="-v"
fi

if test "$verbose" == "1";
then
	echo "Starting backup ..."
fi
#Create month folder
path="/backups/"`date +%Y/%m`
#logfile="/var/log/sysback"
name="backup-"`date +%Y.%m.%d-%H.%M.%S`".tar.gz"
fqn=$path"/"$name
target="/"
admin="admin email address"

#Sending email ...
echo "Backup started in "$fqn" for the whole system ("`du -sh --exclude=/proc --exclude=/backups /`")." 
 | mail -s "Backup started" $admin -a "From: backups@"`hostname`

if test -e $fqn;
then
#	logger -f $logfile -t backup -p daemon.warning "Removing previous backup: "$fqn
	rm -f $fqn
fi
if test ! -e $logfile;
then
	echo "" > $logfile
fi
if test "$verbose" == "1";
then
        echo "Backing up in "$fqn" ..."
fi
#logger -f $logfile -t backup -p daemon.notice "Backing up the contents of '"$target"' to "$fqn
if test "$verbose" == "1";
then
        echo "Setting up folders ..."
fi
mkdir -p /backups/`date +%Y/%m`
su root -c "tar $tarv -Pzcpf $fqn --directory=$target --exclude=proc --exclude=sys --exclude=dev/pts
--exclude=backups --exclude=var/log $target 2> /dev/null" #>(grep -v 'socket ignored' >&2)
rm -f /backups/latest
ln -s $fqn /backups/latest
#logger -f $logfile -t backup -p daemon.notice "Backup completed: "$name
if test "$verbose" == "1";
then
        echo "Backup finished: "`du -sh $fqn`
fi
echo "Backup was completed for the whole system ("`du -sh $fqn`")." | mail -s "Backup finished"
$admin -a "From: backups@"`hostname`

Once executed, this script will backup the target specified by “$target” (the whole filesystem in this case) and create a tar.gz file under “/backups/month/date.tar.gz”. You have to specify an “admin” email account to which notifications will be sent.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I’m a tinkerer and engineer. Here, I write about all sorts of things.

Tags

administration algorithms application bash command blog book client clrs code cormen courses Data Structures domain eyeos firefox google google interview google interview series 1 google interview series 2 hello world host interview interview preparation java job learning linux mac phone screen poem Poetry preparation programming Projects repository Review script shell spring synopsis system management user management video windows 7 writing

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2021 the thinkpod | Built using WordPress and Responsive Blogily theme by Superb