Freebsd backup scripts

FreeBSD Installation tips

Freebsd backup scripts

Postby stephen » Thu Oct 21, 2010 11:39 am

This site has a Freebsd backup script
http://www.xrypto.com/geek/code/freebsd-backup-script-using-rsync.html

Code: Select all
 
PDFPrintE-mail
FreeBSD Backup Script Using RSync
Geek | Code
Written by Brandon J. Yaniz
Tuesday, 10 June 2008 06:53

Backing up your files is vital! I know that I have experienced crashes, bad hard drives, and have even destroyed my system by being stupid. The end result can be a loss of data, and that just sucks, especially when you are self employeed and are being paid to create those files.

It also sucks when you have spent many hours building/modifying applications, only to realize that the path you have chosen is wrong, and that you need to go back to an earlier version of that file, but you can't since you did not back it up.

I got sick of dealing with these situations and so I wrote a little shell script to backup/archive the data of my user folder in FreeBSD.

First, make sure that rsync is installed! This script seems to work well, though I am sure that I still have some cleanup to do.

To use this script create a the folder structure:
~/bin/backup/

Place the code below in a file called backup. Also create a file called exclude and place any folders you don't want to backup into this file, one per line. For example:

.Trash temp Desktop/Video

The above example would tell the script to ignore anything in the folders .Trash, ~/temp/, and ~/Desktop/Video/.

This script looks to backup to the mount point /backup/ so make sure somthing is mounted there else there will be errors and nothing will be backed up.
Backup Script [Download]

#!/bin/sh export PATH=/usr/local/bin:/usr/bin:/bin fsSource="$HOME/" fsExclude="$HOME/bin/backup/exclude" minute=$(date +"%M") hour=$(date +"%H") day=$(date +"%d") weekday=$(date +"%u") week=$(date +"%U") month=$(date +"%m") year=$(date +"%y") fsBase="/backup/"$(whoami) fsHourly="$fsBase/hourly" fsDaily="$fsBase/daily" fsWeekly="$fsBase/weekly" fsWeekday="$fsBase/weekday" fsMonthly="$fsBase/monthly" fsYearly="$fsBase/yearly" if [ -d $fsBase ]; then if [ ! -d $fsHourly ];then mkdir $fsHourly fi if [ ! -d $fsDaily ];then mkdir $fsDaily fi if [ ! -d $fsWeekly ];then mkdir $fsWeekly fi if [ ! -d $fsMonthly ];then mkdir $fsMonthly fi if [ ! -d $fsYearly ];then mkdir $fsYearly fi ## Yearly Backup if [ $day -eq '1' ] && [ $month -eq '1' ] && [ $hour -eq '1' ]; then rsync -av --link-dest=$fsMonthly/current --exclude-from=$fsExclude $fsSource $fsYearly/$year if [ -d $fsYearly/current ]; then rm $fsYearly/current fi ln -s $fsYearly/$year $fsYearly/current fi ## Monthly Backup # Backup if [ $day -eq '1' ]; then rsync -av --link-dest=$fsWeekly/current --exclude-from=$fsExclude $fsSource $fsMonthly/$month if [ -d $fsMonthly/current ]; then rm $fsMonthly/current fi ln -s $fsMonthly/$month $fsMonthly/current # Cleanup dir="$(ls $fsWeekly)" for d in $dir; do rm -r $fsWeekly/$d done fi ## Weekly Backup # Backup if [ $weekday -eq '1' ]; then rsync -av --link-dest=$fsDaily/current --exclude-from=$fsExclude $fsSource $fsWeekly/$week if [ -d $fsWeekly/current ]; then rm $fsWeekly/current fi ln -s $fsWeekly/$week $fsWeekly/current # Cleanup dir="$(ls $fsDaily)" for d in $dir; do rm -r $fsDaily/$d done fi ## Daily Backup # Backup if [ $hour -eq '1' ]; then rsync -av --link-dest=$fsHourly/current --exclude-from=$fsExclude $fsSource $fsDaily/$day if [ -d $fsDaily/current ]; then rm $fsDaily/current fi ln -s $fsDaily/$day $fsDaily/current # Cleanup dir="$(ls $fsHourly)" for d in $dir; do rm -r $fsHourly/$d done fi ## Hourly Backup # Backup if [ -d $fsHourly/current ]; then rsync -av --link-dest=$fsHourly/current --exclude-from=$fsExclude $fsSource $fsHourly/$hour else rsync -av --link-dest=$fsDaily/current --exclude-from=$fsExclude $fsSource $fsHourly/$hour fi if [ -d $fsHourly/current ]; then rm $fsHourly/current fi ln -s $fsHourly/$hour $fsHourly/current fi

Now add the following line to your crontab

0 * * * * ~/bin/backup/backup

This script works by creating directories hourly, daily, weekly, monthly, and yearly. Using rsync the script will:

    * Every Hour, create an hourly backup
    * At 1:00 am, create a daily backup deleting all previous hourly backups
    * At 1:00 am on Monday, create a weekly backup deleting all previous daily backups
    * At 1:00 am, first of the Month, create a monthly backup deleting all previous weekly backups
    * At 1:00 am, first of the year, create a yearly backup deleting all previous monthly backups

A nice feature of rsync is that we can backup all changed files, without duplicating unchanged files, helping to reduce overall backup size.


Digg This! Del.icio.us; Reddit Seed Newsvine Google Stumble It! Technorati Facebook
 
© 2005 - 2010 Brandon J. Yaniz, All Rights Reserved
Valid XHTML and CSS.

    * Home
    * Thoughts
    * Projects
    * Experiments
    * Geek

Xrypto
stephen
 
Posts: 507
Joined: Thu Feb 09, 2006 9:37 am
Location: Brisbane

Return to FreeBSD

Who is online

Users browsing this forum: No registered users and 1 guest