-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-backup.sh
executable file
·47 lines (47 loc) · 1.58 KB
/
basic-backup.sh
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
#!/usr/bin/env bash
#####
# Purpose: Backup a specific directory and delete old backups
#####
# Please change the four variables below according to your needs
#####
SERVICE="dotfiles" # what the finished backup fill will be called
DIRTOBACKUP="/home/mdorner/dotfiles/" # the directory you want to backup
BACKUPDIR="/mnt/backup1.moxli.de/dotfiles/" # where the backup file will be stored
KEEP=2 # the number of backups you want to keep
#####
DATE=$(date +%Y%m%d%H%M%S)
BACKUPROOT=$(dirname $DIRTOBACKUP)
SERVICEDIR=$(basename $DIRTOBACKUP)
#####
trap clean_up SIGHUP SIGINT SIGQUIT SIGTERM
#####
function clean_up() {
echo "you propably hit Ctrl-C or I received a signal(HUP/INT/QUIT/TERM)"
echo "removing the temporary files..."
rm -f "$BACKUPDIR.$SERVICE-${DATE}.tar.gz_INPROGRESS"
echo "successfully removed the temporary file"
echo "exiting now!"
exit 1
}
#####
if [ ! -d "$BACKUPDIR" ]
then
echo "The directory to backup to($BACKUPDIR) does not exist."
echo "Creating $BACKUPDIR..."
if [ "$(mkdir -p $BACKUPDIR)" ]
then
exit 1
else
echo "Created $BACKUPDIR successfully."
fi
fi
BACKUPS=$(find "$BACKUPDIR" -name "$SERVICE-*.tar.gz" | wc -l | sed 's/\ //g')
while [ "$BACKUPS" -ge "$KEEP" ]; do
ls -tr1 "$BACKUPDIR$SERVICE"-*.tar.gz | head -n 1 | xargs rm -f
BACKUPS=$(("$BACKUPS" -1))
done
rm -f "$BACKUPDIR.$SERVICE-${DATE}.tar.gz_INPROGRESS"
cd "$BACKUPDIR" || exit 1
tar -cpzf ".$SERVICE-${DATE}.tar.gz_INPROGRESS" -C "$BACKUPROOT" "$SERVICEDIR"
mv -f "$BACKUPDIR.$SERVICE-${DATE}.tar.gz_INPROGRESS" "$BACKUPDIR$SERVICE-${DATE}.tar.gz"
exit 0