-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsystem_backup.sh
48 lines (40 loc) · 1.15 KB
/
system_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
#!/bin/bash
#
# Automate Raspberry Pi Backups
#
# Kristofer Källsbo 2017 www.hackviking.com
#
# Usage: system_backup.sh {path} {days of retention}
#
# Below you can set the default values if no command line args are sent.
# The script will name the backup files {$HOSTNAME}.{YYYYmmdd}.img
# When the script deletes backups older then the specified retention
# it will only delete files with it's own $HOSTNAME.
#
# Declare vars and set standard values
backup_path=/mnt/backup
retention_days=3
# Check that we are root!
if [[ ! $(whoami) =~ "root" ]]; then
echo ""
echo "**********************************"
echo "*** This needs to run as root! ***"
echo "**********************************"
echo ""
exit
fi
# Check to see if we got command line args
if [ -n "$1" ]; then
backup_path=$1
fi
if [ -n "$2" ]; then
retention_days=$2
fi
# Create trigger to force file system consistency check if image is restored
touch /boot/forcefsck
# Perform backup
dd if=/dev/mmcblk0 of="$backup_path"/"$HOSTNAME"."$(date +%Y%m%d)".img bs=1M
# Remove fsck trigger
rm /boot/forcefsck
# Delete old backups
find "$backup_path"/"$HOSTNAME".*.img -mtime +"$retention_days" -type f -delete