-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·46 lines (35 loc) · 1.23 KB
/
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
#!/bin/bash
set -uo pipefail
SCRIPT_PATH=$(realpath "$0")
BACKUP_DISK_LABEL="fedora-backup"
if [[ "$#" -ge 1 && "$1" == "--install" ]]; then
echo "Setup cron schedule"
backupCronEntry="0 12 * * 1-5 $SCRIPT_PATH"
cronEntries=$(crontab -l)
hasExistingCronEntries=$(("$?" == "1"))
if echo "$cronEntries" | grep "$backupCronEntry";
then
hasBackupCronEntry="1"
else
hasBackupCronEntry="0"
fi
if [[ $hasExistingCronEntries == "1" && $hasBackupCronEntry == "1" ]]; then
echo "Backup cron entry $backupCronEntry already installed"
exit 0
fi
tempfile=$(mktemp "/tmp/backupcron.XXXXXXX")
if [[ "$hasExistingCronEntries" == "1" ]]; then
echo "$cronEntries" >> "$tempfile"
fi
echo "$backupCronEntry" >> "$tempfile"
crontab "$tempfile"
rm "$tempfile"
echo "Installed cron entry $backupCronEntry"
exit 0
fi
if [[ ! -e "/dev/disk/by-label/$BACKUP_DISK_LABEL" ]]; then
echo "Backup disk $BACKUP_DISK_LABEL not connected, skipping backup!"
exit 0
fi
rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /run/media/k3v1n/fedora-backup/thinkpadl470
echo "Backup to $BACKUP_DISK_LABEL finished"