Make a Backup

After two harddrives died on me, I decided that it was time for a backup. So I started to browse around which tools to use &c. In the end, it seemed to me that the best (and most reliable) way was to rely on well-known tools like “tar”.

Everyone knows “tar” which originally was designed to make archives on tapes. It is a great tool, however there is also “dar” which has some advantages when you want to make a backup of your disk: It seems to be able to handle better incremental backups, it can compress (gzip or bzip2) on the fly, and it can stop every xx MB so that you can burn the resulting file to a CD or DVD.

There are GUIs like KDAR for KDE, as far as I am informed — however, it is also possible to do all you need from the command line (as always…)

I wrote two scripts: the first one will create a backup of your home directory. The filenames will contain the date to make sure you know what is what later on. The backup will be split into files of 1GB size. There is a filesizelimit you can burn on a DVD, that’s why I was not able to burn the 4GB files I created in the first attempt. After every 4th file, the script will pause until you make it run again — that gives time to burn the four files and erase then from the disk so that you have enough disk space. Also a catalogue file is created which will eventually serve as reference for a differental backup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env bash
 
### make a backup with dar and burn it on a dvd
# the maximum file size to burn on a dvd using growisofs is below 4GB due to a 32bit address system.
 
cd ~/
 
read -p "full or differential backup? [f/d] " fulldiff
 
### do the backup with dar. make 1GB slices, pause after 4 to allow the user to burn and remove
# IF first, full backup:
if [ "$fulldiff" == "f" ]
then
	sudo dar -c /var/archives/`date -I`_full -G /var/archives/`date -I`_cata -P .Trash/ -z -s 1G -p4 -b
# ELSE subsequent differential backup:
elif [ "$fulldiff" == "d" ]
then
	prevbackup=`ls -t /var/archives/*_cata*.dar | head -1`
	sudo dar -c /var/archives/`date -I`_diff -G /var/archives/`date -I`_cata -P .Trash/ -z -s 1G -p4 -b -A ${prevbackup%%.*r}
else
	echo "this is not an option"
	exit 2
fi
 
# extract the catalogue, suppress any "no such file or directory" error message
# 
#backupname=`ls -t /var/archives/*_full*.dar /var/archives/*_diff*.dar 2>/dev/null | head -1`
#sudo dar -C /var/archives/`date -I`_cata -A ${backupname%%.*r}
 
exit 0

Now here comes the second script which burns the backup on a DVD. As soon as the other script pauses, I run this one. It is longish, most of it just tries to decide if there is still enough space on the DVD. It will erase you backup file from the harddrive after burning!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
 
### make a backup with dar and burn it on a dvd
# the maximum file size to burn on a dvd using growisofs is below 4GB due to a 32bit address system.
 
cd ~/
 
### now transfer the backup to dvd
mediainfo=`dvd+rw-mediainfo /dev/dvd`
discstat=`echo "$mediainfo" | grep "Disc status" | awk {'print $3'}`
if [ ! -n "$discstat" ]
then
        echo "There seems to be no disc in the drive"
        echo $mediainfo
        exit 1
fi
 
# get the names of the backups, suppress any "no such file or directory" error message
backupname=`ls -t /var/archives/*_full*.dar /var/archives/*_diff*.dar 2>/dev/null`
echo "burning the file(s) "$backupname
backupsize=0
for file in $backupname
do
        let backupsize=$backupsize+`ls -l $file | awk {'print $5'}`
done
 
discfree=`echo "$mediainfo" | grep "Free Blocks" | tail -n 1 | awk {'print $3'}`
let discfree=${discfree%%'*2KB'}*2*1024
 
a="y"
while [ "$a" == "y" ]
do
        # IF dvd is empty burn dvd
        if [ "$discstat" == "blank" ]
        then
                echo "blank dvd"
                if [ "$discfree" -gt "$backupsize" ]
                then
                        growisofs -Z /dev/dvd -R -V backup $backupname
                        # erase the backup from the disk
                        sudo rm $backupname
                        exit 0
                else
                        echo "the size of the backup is"
                        echo $backupsize
                        echo "and the free space on the dvd is only"
                        echo $discfree
                fi
        # ELSE append data to dvd
        elif [ "$discstat" == "appendable" ]
        then
                echo "appendable dvd"
                if [ "$discfree" -gt "$backupsize" ]
                then
                        growisofs -M /dev/dvd -R $backupname
                        # erase the backup from the disk
                        sudo rm $backupname
                        exit 0
                else
                        echo "the size of the backup is"
                        echo $backupsize
                        echo "and the free space on the dvd is only"
                        echo $discfree
                        read -p "try again [y] or not [n]? " a
                fi
        else
                echo "there is something wrong with the dvd"
                echo "$mediainfo"
                read -p "try again [y] or not [n]? " a
        fi
done
 
exit 1

I am pretty sure that there are still several bugs in here — if you find one, please let me know!

Sebastian.

Leave a Reply