Skip to content

Commit c760c0a

Browse files
authored
Create backup.sh
1 parent 422b9ee commit c760c0a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

backup.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
LOGFILE=/var/log/backupjob.log
4+
LASTRUNFILE=lastbackup.txt
5+
6+
echo "Starting job at $(date +%d-%m-%y/%H:%M:%S)" >> $LOGFILE
7+
echo "Checking if host is online ..." >> $LOGFILE
8+
if ping -c 1 192.168.1.201 &> /dev/null
9+
then
10+
echo "Checking last time this job was run ..." >> $LOGFILE
11+
if [ ! -f $LASTRUNFILE ]; then
12+
echo "Could not determine last run" >> $LOGFILE
13+
LASTRUN=0
14+
else
15+
LASTRUN="`cat $LASTRUNFILE`"
16+
fi
17+
echo $LASTRUN
18+
19+
# If the last backup was run more than 12 hours ago
20+
CURRENT_TIME=$(date +%s)
21+
CHECKING_TIME=$(($LASTRUN+43200))
22+
if [ "$CURRENT_TIME" -gt "$CHECKING_TIME" ] ; then
23+
echo "Host is online, trying to mount directory ..." >> $LOGFILE
24+
sudo mount -t cifs -o username=Daan,password=Iceman1122 //192.168.1.201/Backup /mnt/win
25+
echo "Checking if directory is successfully mounted ..." >> $LOGFILE
26+
if mountpoint -q /mnt/win; then
27+
echo "Directory is mounted" >> $LOGFILE
28+
echo "Syncing folders ..." >> $LOGFILE
29+
sleep 1
30+
sudo rsync -avz --delete "/mnt/600gb_external/600gb_share/Backup" "/mnt/win"
31+
echo "Done" >> $LOGFILE
32+
echo "$(date +%s)" >> $LASTRUNFILE
33+
else
34+
echo "Windows PC is not mounted, cannot run backup" >> $LOGFILE
35+
fi
36+
else
37+
echo "Last run was less than 12 hours ago, exiting ..." >> $LOGFILE
38+
fi
39+
else
40+
echo "Host is offline, removing mount if there is any ..." >> $LOGFILE
41+
sudo umount -f -l /mnt/win
42+
fi
43+
44+
echo "End." >> $LOGFILE
45+
46+
47+

0 commit comments

Comments
 (0)