Skip to content

Instantly share code, notes, and snippets.

@eyJhb
Created November 14, 2018 21:20
Show Gist options
  • Save eyJhb/aa39c77d5e665bb8ed05218de81c60a7 to your computer and use it in GitHub Desktop.
Save eyJhb/aa39c77d5e665bb8ed05218de81c60a7 to your computer and use it in GitHub Desktop.
Simple script I use to backup my servers - uses PGP for encryption and SCP for uploading
#!/bin/bash
# Linux SCP Backup Script
# how it works
# -
#date string
d=$(date --iso)
# dir where backup is located
BACKUP_TEMP_DIR="/root"
BACKUP_TEMP_FOLDER="$BACKUP_TEMP_DIR/backup-$d"
# remote dir
REMOTE_DIR="./"
backup_clean() {
echo 'Removing local backup folder'
rm -rf $BACKUP_TEMP_DIR/backup-*
}
PGP_ID="E888888B"
SCP_USER="rssh"
SCP_KEY_FILE="/root/.ssh/id_rsa_backup"
SERVER_IP="172.20.0.6"
SERVER_LOCATION="/media/backup/location/"
PORT=2222
backup_make() {
mkdir "$BACKUP_TEMP_FOLDER"
#backup /etc/openvpn folder
tar -czf "$BACKUP_TEMP_FOLDER/etc-openvpn.tar.gz" /etc/openvpn/
#backup /home/user/openvpn-ca folder
tar -czf "$BACKUP_TEMP_FOLDER/home-user-openvpn-ca.tar.gz" /home/eyjhb/openvpn-ca/
#tar folder into new .tar.gz with correct filename inside root
tar -cz -C $BACKUP_TEMP_DIR backup-$d/ | gpg -r $PGP_ID -o "$BACKUP_TEMP_DIR/backup-$d.tar.gz" --encrypt
echo "Beginning ftp upload"
scp -P $PORT -i $SCP_KEY_FILE $BACKUP_TEMP_DIR/backup-$d.tar.gz $SCP_USER@$SERVER_IP:$SERVER_LOCATION
}
#clean all files left behind from this script
backup_clean
#make the backup
backup_make
#clean up after us again
backup_clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment