# $USER backup script



## Michael-Sanders (Jan 16, 2013)

Hi guys. Here's a simple backup script to help archive the contents of your home directory. Currently, it supports six different archiving methods:

tar/bzip, tar/xz, tar/gzip, tar/t7z, rar, zip

If you have any ideas to make it better, I'm all ears...


```
#!/bin/sh

# This script provides a rolling backup scheme for your home directory & prunes
# archives greater than 7 days old, example output: ~/backup/mike-20130101.zip
#
# |||(__) ^  FreeBSD ~/backup script
#  | (  )(   [c]2013 Topcat Software LLC.
#  |--  >_)  http://topcat.hypermart.net 
#  | _||_    All rights reserved. 

# ----------------------------------------------------------------------------

# path to backup directory & name of archive

DIR=$HOME/backup
OBJ=$DIR/$USER-`date "+%Y%m%d"`

# ----------------------------------------------------------------------------

# create ~/backup directory if it doesn't exist

[ -d $DIR ] || mkdir $DIR

# ----------------------------------------------------------------------------

# prune backups, choose one extension of .tbz .txz .tgz .t7z .rar .zip

find $DIR/ -type f -iname $USER-'[0-9]*'.zip -maxdepth 1 -mtime +7 -delete

# ----------------------------------------------------------------------------

# choose archive type: tar/bzip tar/xz tar/gzip tar/7zip rar zip

#tar --exclude=$DIR -jcvf $OBJ.tbz $HOME
#tar --exclude=$DIR --options xz:9 --xz -cvf $OBJ.txz $HOME
#tar --exclude=$DIR --options gzip:9 -zcvf $OBJ.tgz $HOME
#tar --exclude=$DIR -cvf - $HOME | 7za a -mx=9 -si $OBJ.t7z
#rar a -m5 -s -r -ol -x$DIR/ $OBJ.rar $HOME
zip -9r -x$DIR/\* $OBJ.zip $HOME

# ----------------------------------------------------------------------------

# choose notification method (syslog or mail) if backup fails

#ERR=$?; [ $ERR -eq 0 ] || logger -t backup error: $ERR
ERR=$? ; [ $ERR -eq 0 ] || echo backup error: $ERR | mail -s backup $USER

# eof
```


----------



## fonz (Jan 16, 2013)

Michael-Sanders said:
			
		

> If you have any ideas to make it better, I'm all ears...


I'm not usually awake this time of day, but here are a few (sleepy) thoughts:
The backups appear to include the backup directory and any older backups therein, which may or may not be what you want.
You might want to add command line arguments that allow the user of the script to choose the backup format (e.g. *-z* for zip) and/or the way to report errors.
tar(1) and archivers/zip can both list the files they are archiving, which you could use to (perhaps optionally) generate a table of contents.
I recommend checking whether $HOME exists and is not empty.
Fonz


----------



## Michael-Sanders (Jan 16, 2013)

fonz said:
			
		

> I'm not usually awake this time of day, but here are a few (sleepy) thoughts:
> The backups appear to include the backup directory and any older backups therein, which may or may not be what you want.
> You might want to add command line arguments that allow the user of the script to choose the backup format (e.g. *-z* for zip) and/or the way to report errors.
> tar(1) and archivers/zip can both list the files they are archiving, which you could use to (perhaps optionally) generate a table of contents.
> ...



Hi Fonz.

Well... actually the script specifically does NOT include the backup directory when archiving (see args --exclude, and -x). But some good ideas elsewise =)


----------



## fonz (Jan 16, 2013)

Michael-Sanders said:
			
		

> Well... actually the script specifically does NOT include the backup directory when archiving (see args --exclude, and -x)


You're right, I see it now. Told you I was sleepy 

Fonz


----------



## Michael-Sanders (Jan 16, 2013)

Chuckle, we're only human. Honestly (coming from Windows to FreeBSD), I have other questions I know I'll need help with soon...


----------

