# Backup Scripts



## newton (May 4, 2012)

Hi,

I've got some backup scripts that aren't running right, and could use some help getting them working.  Here's what I've got.

On the machine to receive the backup

`crontab -e`

```
@daily ~/backup.sh
```

~/backup.sh

```
#!/usr/local/bin/bash
#dump svn repos
ssh backup@nas2 '/home/backup/prep_backup.sh' >> ~/backup.log

#dump each of the repositories, one by one
~/zfs_backup.sh nas2 zfile/tina zfile >> ~/backup.log
~/zfs_backup.sh nas2 zfile/josh zfile >> ~/backup.log
~/zfs_backup.sh nas2 zfile/maveric zfile >> ~/backup.log
~/zfs_backup.sh nas2 zfile/dump zfile >> ~/backup.log
~/zfs_backup.sh nas2 zfile/svn zfile >> ~/backup.log
~/zfs_backup.sh nas2 zfile/git zfile >> ~/backup.log
```

~zfs_backup.sh

```
#!/usr/local/bin/bash
set -x
host=$1
pool=$2
destination=$3

#dates
today=`date +"$type%Y_%m_%d"`
yesterday=`date -v -1d +"$type%Y_%m_%d"`

#snapshot names
snapshot_today="$pool@$today"
snapshot_yesterday="$pool@$yesterday"

# look for a snapshot with this name
if ssh $host "sudo zfs list -H -o name -t snapshot | sort | grep $snapshot_today > /dev/null"; then
echo " snapshot, $snapshot_today, already exists, removing"
ssh $host "sudo zfs destroy $snapshot_today"
fi
echo " taking todays snapshot, $snapshot_today"
ssh $host "sudo zfs snapshot -r $snapshot_today"

# look for yesterday snapshot
if ssh $host zfs list -H -o name -t snapshot | sort | grep "$snapshot_yesterday$" > /dev/null; then
echo " yesterday snapshot, $snapshot_yesterday, exists lets proceed with backup"
ssh $host "sudo zfs send -i $snapshot_yesterday $snapshot_today" | zfs receive -d $destination
exit 0
else
echo " missing yesterday snapshot aborting, $snapshot_yesterday"
exit 1
fi
set +x
```

On the machine to send the backup:

~/home/backup

```
#!/usr/local/bin/bash
set -x

echo "nas2.newton"
echo "prepare backup"
svn_dir=/zfile/svn/*
save_dir=/zfile/dump/svn_dump/
for f in $svn_dir;
do
        if [ -d "$f" ]; then
                echo Dumping "${f##*/}"
                svnadmin dump -q $f > "$save_dir"/"${f##*/}".dmp
        fi;
done

set +x
```

And the error from my log: 


```
+ echo ' yesterday snapshot, zfile/dump@2012_03_24, exists lets proceed with backup'
+ ssh nas2 'sudo zfs send -i zfile/dump@2012_03_24 zfile/dump@2012_03_25'
/home/backup/zfs_backup.sh nas2 zfile/maveric zfile
```


----------



## SirDice (May 4, 2012)

I might be missing something but I don't see any errors in the bit you posted.


----------



## newton (May 4, 2012)

SirDice,

Thanks for looking.  Seems *I* have a copy and paste error.  The error was 
	
	



```
command zfs not found
```
 or something to that effect, right after the lines I posted in the "error" section.  When I get a chance I'll copy it exactly.


----------



## SirDice (May 4, 2012)

Keep in mind that scripts running from cron(8) have a limited PATH. This is the most common reason why scripts don't work.


----------



## phoenix (May 4, 2012)

The Interrupted UNIX FAQ


----------



## redw0lfx (May 4, 2012)

You most likely need to use /sbin/zfs instead of just zfs in your scripts.  Or another option is to configure PATH in your crontab(5) so that you don't have to remember to spell out a full path for your commands.

[CMD=""]crontab -e[/CMD]

```
PATH=$PATH:/sbin:/usr/local/sbin
@daily ~/backup.sh
```


----------



## _martin (May 20, 2012)

phoenix said:
			
		

> The Interrupted UNIX FAQ



Your first FAQ is hilarious. It very well works with NFS too


----------



## UNIXgod (May 20, 2012)

matoatlantis said:
			
		

> Your first FAQ is hilarious. It very well works with NFS too



It's wblock's faq. I agree. It's very well written. Saved my ass a couple of times.


----------



## wblock@ (May 20, 2012)

Sometimes I wonder if it's too wordy.


----------

