Skip to content

Instantly share code, notes, and snippets.

@SergioLuis
Last active January 19, 2019 16:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergioLuis/18df57d1098443bb6e5ba4836287bd93 to your computer and use it in GitHub Desktop.
Save SergioLuis/18df57d1098443bb6e5ba4836287bd93 to your computer and use it in GitHub Desktop.
A bash script to automatically backup Plastic SCM's backend files to OneDrive.
#!/bin/bash
# A simple script to backup Plastic SCM's backend data, notifying IFTTT in the
# process.
#
# Usage: ./backupdatabases.sh <encrypt password>
#
# Author: Sergio Luis Para
# Year: 2018
# CUSTOMIZE THESE FIELDS AS NEEDED.
IFTTTKEY="" # Your IFTTT key to connect to the maker API.
# You can get yours clicking on "Documentation" at:
# https://ifttt.com/maker_webhooks
TEMP="/tmp/" # Temporal path to store intermediate files.
JETFILES="/var/lib/plasticscm/jet/" # Jet backend files' location.
REMOTEPATH="od:/plasticbackups" # OneDrive path to store the backups.
DATEFORMAT="$(date +'%Y%m%d_%H%M%S')" # Backup filename formats.
ZIPFILE="$TEMP$DATEFORMAT".tar.7z
GPGFILE="$ZIPFILE".gpg
IFTTT_START="https://maker.ifttt.com/trigger/plastic_backup_start/with/key/$IFTTTKEY"
IFTTT_SUCCESS="https://maker.ifttt.com/trigger/plastic_backup_success/with/key/$IFTTTKEY"
IFTTT_FAIL="https://maker.ifttt.com/trigger/plastic_backup_fail/with/key/$IFTTTKEY"
function notify {
if [ $# -eq 1 ];
then
BODY="{\"value1\" : \"$HOSTNAME\"}"
URL=$1
else
BODY="{\"value1\" : \"$HOSTNAME\", \"value2\" : \"$1\"}"
URL=$2
fi
echo $BODY
echo $URL
curl -s --header "Content-Type: application/json" -d "$BODY" $URL >/dev/null
}
function checkError {
if [ $? -ne 0 ];
then
echo $1
notify "$1" "$2"
exit 1
fi
}
if [ "$#" -ne 1 ];
then
echo "Usage: ./backupdatabases.sh <encrypt password>"
exit 1
fi
PASS=$1
if [ ! -d $TEMP ];
then
ERROR_MSG="Temporal dir $TEMP does not exist."
echo $ERROR_MSG
notify "$ERROR_MSG" "$IFTTT_FAIL"
exit -1
fi
echo "$(date +'%d/%m/%Y %H:%M:%S') STARTING BACKUP"
notify "$IFTTT_START"
cm admin readonly enter
if [ $? -ne 0 ] && [ "$(pidof plasticd)" != "" ];
then
ERROR_MSG="The Plastic SCM Server is running, but could not enter readonly mode."
notify "$ERROR_MSG" "$IFTTT_ERROR"
exit 1
fi
tar cf - $JETFILES | 7za a -t7z -m0=lzma -mx=5 -mfb=64 -md=32m -ms=on -si $ZIPFILE
checkError "Error generating tar archive." "$IFTTT_FAIL"
cm admin readonly leave
echo $PASS | gpg --batch --yes --passphrase-fd 0 -c $ZIPFILE
checkError "Error encrypting tar archive." "$IFTTT_FAIL"
onedrivecmd put $GPGFILE $REMOTEPATH
checkError "Error uploading backup to OneDrive." "$IFTTT_FAIL"
echo "$(date +'%d/%m/%Y %H:%M') BACKUP FINISHED"
rm "$TEMP"*
notify "$IFTTT_SUCCESS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment