Current location

narf Source control manager Git

summaryrefslogtreecommitdiff
blob: 0e0a0c1fa4e65ab9fa932571cf9cd6cb2291972d (plain)
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
#!/bin/bash
TOSAVE="/home /etc /var/cvsroot /var/svn /var/www /var/yp"
EXCLUDES="--exclude /home/guest --exclude /home/ftp --exclude /home/gullutc --exclude */download/* --exclude */nosave/* --exclude */lost+found/*"
USETRASH=NO # NO to force automatic deletion

BACKUP_FOLDER=/data/backup
WBACKUP_FOLDER=${BACKUP_FOLDER}/weekly
MBACKUP_FOLDER=${BACKUP_FOLDER}/monthly
TRASH_FOLDER=${BACKUP_FOLDER}/trash

RSYNC=/usr/bin/rsync
DATE=/bin/date
TAR=/bin/tar
FIND=/usr/bin/find
CP=/bin/cp
RM=/bin/rm
MV=/bin/mv
LN=bin/ln
BASENAME=/usr/bin/basename

NOWSTRING=`$DATE +%Y%m%d`
WEEKDAY=`$DATE +%u`
MONTHDAY=`$DATE +%d`

# daily backup
for dir in $TOSAVE ; do
	$RSYNC -a --delete-excluded --delete ${EXCLUDES} $dir $BACKUP_FOLDER
done

# weekly saves on mondays
if [ "$WEEKDAY" == "1" ]; then
	for dir in $TOSAVE ; do
		# remove saves older than 4 weeks ago (31 days)
		if [ "$USETRASH" = "NO" ]; then
			$FIND ${WBACKUP_FOLDER} -ctime +31 -exec $RM \{\} \;
		else
			$FIND ${WBACKUP_FOLDER} -ctime +31 -exec $MV \{\} /tmp \;
		fi
		# make new backups
		pushd ${BACKUP_FOLDER} > /dev/null
		$TAR cjf ${WBACKUP_FOLDER}/${dir//\//_}-${NOWSTRING}.tar.bz2 ./`$BASENAME ${dir}`
		popd > /dev/null

		# monthly saves
		if [ $MONTHDAY -le 7 ]; then
			# remove saves older than 3 months ago (93 days)
			if [ "$USETRASH" == "NO" ]; then
				$FIND ${MBACKUP_FOLDER} -ctime +93 -exec $RM \{\} \;
			else
				$FIND ${MBACKUP_FOLDER} -ctime +93 -exec $MV \{\} /tmp \;
			fi
			# copy new monthly backup
			$MV ${WBACKUP_FOLDER}/${dir//\//_}-${NOWSTRING}.tar.bz2 ${MBACKUP_FOLDER}	
			pushd ${WBACKUP_FOLDER} > /dev/null
			$LN -s ${MBACKUP_FOLDER}/${dir//\//_}-${NOWSTRING}.tar.bz2
			popd > /dev/null
		fi
	done
fi