-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackup.sh
90 lines (72 loc) · 2.17 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
#
# Raspberry Pi Backup Script
#
#
# Configuration Section:
# Backup Directory
DIR=/media/backups/
# Services to pause
SERVICES="php5-fpm bitlbee mysql nginx unbound cron samba vsftpd ssh vncserver"
echo "Starting RaspberryPI backup process!"
# Check if backup directory exists
if [ ! -d "$DIR" ];
then
echo "Backup directory $DIR doesn't exist, creating it now!"
mkdir $DIR
fi
# Create a filename with datestamp for our current backup (without .img suffix)
OFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S)"
# Create final filename, with suffix
OFILEFINAL=$OFILE.img
#stop running processes
for svc in $SERVICES
do
/etc/init.d/$svc stop
done
#kill emulationstation, if running
es=$(pgrep emulationstation)
if [ ! -z "$es" ]; then
kill `pidof emulationstation`;
fi;
#disable swap
dphys-swapfile swapoff
# First sync disks
sync; sync
# Begin the backup process, should take about 1 hour from 8Gb SD card to HDD
echo "Backing up SD card to USB HDD."
echo "This will take some time depending on your SD card size and read performance. Please wait..."
SDSIZE=`blockdev --getsize64 /dev/mmcblk0`;
pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock
# Wait for DD to finish and catch result
RESULT=$?
# Start services again that where shutdown before backup process
echo "Start the stopped services again."
#re-enable swap
dphys-swapfile swapon
for svc in $SERVICES
do
/etc/init.d/$svc start
done
#if it was running, re-enable emulationstation
if [ ! -z "$es" ]; then
emulationstation &;
fi;
# If command has completed successfully, delete previous backups and exit
if [ $RESULT = 0 ];
then
echo "Successful backup, previous backup files will be deleted."
rm -f $DIR/backup_*.gz
mv $OFILE $OFILEFINAL
echo "Backup is being gzipped. Please wait..."
gzip $OFILEFINAL
echo "RaspberryPI backup process completed! FILE: $OFILEFINAL.tar.gz"
exit 0
# Else remove attempted backup file
else
echo "Backup failed! Previous backup files untouched."
echo "Please check there is sufficient space on the HDD."
rm -f $OFILE
echo "RaspberryPI backup process failed!"
exit 1
fi