-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgpg-backup.sh
executable file
·47 lines (45 loc) · 1.33 KB
/
gpg-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
#!/bin/bash
# Script to copy sensitive files
#
# Files are put in an archive and then GPG protected
# Put this script in a crontab
# 30 21 * * * /Users/me/Scripts/gpg-backup.sh > /dev/null 2>&1
#
# Koen Van Impe 20141108
#
BACKUP_HOST="<hostname>"
BACKUP_USER="<username>"
BACKUP_KEY="<privatekey>"
BACKUP_PATH="<pathtobackupto>"
GPG_RECIPIENT="<gpgRCPT>"
GPG_OUTPUT="backup.tar.gz.gpg"
# add $(date +%Y-%m-%d) to GPG_OUTPUT for unique backups
declare -a FILES=( '/home/user/.gnupg/'
'/home/user/.ssh/' )
if [ ${#FILES[@]} -gt 0 ]
then
if [ -f $BACKUP_KEY ]
then
# Check SSH connection
ssh -q -o "BatchMode=yes" -i $BACKUP_KEY $BACKUP_USER@$BACKUP_HOST "echo 2>&1"
CONN=$?
if [ $CONN == 0 ]
then
# Build up filelist to backup
for file in "${FILES[@]}"
do
BACKUP_FILES="$BACKUP_FILES $file"
done
rm $GPG_OUTPUT
tar -cz $BACKUP_FILES | gpg --encrypt --recipient $GPG_RECIPIENT > $GPG_OUTPUT
scp -i $BACKUP_KEY $GPG_OUTPUT $BACKUP_USER@$BACKUP_HOST:$BACKUP_PATH/
rm $GPG_OUTPUT
else
echo "SSH connection to $BACKUP_HOST failed"
fi
else
echo "Private key $BACKUP_KEY not found"
fi
else
echo "No files to backup"
fi