Skip to content

Instantly share code, notes, and snippets.

@JCotton1123
Last active August 29, 2015 14:08
Show Gist options
  • Save JCotton1123/b39cde4f3da4706161b9 to your computer and use it in GitHub Desktop.
Save JCotton1123/b39cde4f3da4706161b9 to your computer and use it in GitHub Desktop.
Cloud Files backup script - written in sh
#!/bin/sh
# Backup script for uploading backups to Cloud Files
# Intended for old servers where other sol'ns won't run.
# Notes:
# * Splits files so they do not exceed the 5GB limit
# * Sets uploaded files to be deleted automatically to maintain a
# proper backup window.
# * Uploads all files as dynamic objects (google this if you care)
# The MIT License (MIT)
#
# Copyright (c) 2014 Jesse Cotton <jcotton1123@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Rackspace info
ACCOUNT= #Tenant ID
USERNAME=
API_KEY=
CF_ENDPOINT="https://storage101.iad3.clouddrive.com/v1/MossoCloudFS_${ACCOUNT}"
CONTAINER=backup-$(hostname)
# Backup info
BACKUP_FILES="/usr/backup/mysql.sql /usr/backup/home.tgz"
CHUNK_SIZE=256m
DELETE_AFTER="2592000" #seconds
# GPG info
PASSPHRASE="abcdefghijk123456"
function main() {
for f in $BACKUP_FILES; do
backupFile "$f"
[ $? -ne 0 ] && echo "Failed to backup file ${f}"
done
}
function backupFile() {
# entry point
file_path="$1"
filename=$(basename $file_path)
# Encrypt file
result=$(
echo "$PASSPHRASE" | \
gpg --batch \
-z 9 \
--cipher-algo AES256 \
--output "${file_path}.gpg" \
--symmetric \
--passphrase-fd 0 \
"${file_path}"
)
[ $? -ne 0 ] && return 1
# Upload new file
uploadFile "${file_path}.gpg" "${filename}.gpg"
[ $? -ne 0 ] && return 2
# Delete the temporary encrypted backup file
rm -f "${file_path}.gpg"
[ $? -ne 0 ] && return 3
}
function uploadFile() {
# Upload a file to Cloud Files
src=$1
dst=$2
obj_uri="$CF_ENDPOINT/$CONTAINER/$dst"
rel_obj_uri="$CONTAINER/$dst"
# Split the file
parts_dir="${src}.parts"
mkdir -p "$parts_dir"
split -b "$CHUNK_SIZE" "$src" "$parts_dir/"
# Get an API token
token=$(getToken)
[ $? -ne 0 ] && return 1
# Upload the file parts
counter=1
for part in $(ls $parts_dir); do
local_part_path="$parts_dir/$part"
remote_part_uri="$obj_uri/$counter"
result=$(
curl --silent --fail -X PUT \
-H "X-Auth-Token: $token" \
-H "X-Delete-After: ${DELETE_AFTER}" \
"$remote_part_uri" \
--data-binary @"$local_part_path"
)
[ $? -ne 0 ] && return 2
counter=$[counter+ 1]
done
# Upload the file manifest
result=$(
curl --silent --fail -X PUT \
-H "X-Auth-Token: $token" \
-H "X-Object-Manifest: ${rel_obj_uri}" \
-H "X-Delete-After: ${DELETE_AFTER}" \
"$obj_uri" \
--data-binary ""
)
[ $? -ne 0 ] && return 2
# Delete the split file parts
rm -rf "${parts_dir}"
return 0
}
function getToken() {
# Retrieve an API token
parse_token='"token":\{"id":"([0-9a-z]+)",'
result=$(
curl --silent --fail -X POST \
-H "Content-type:application/json" \
-d "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username\":\"${USERNAME}\",\"apiKey\":\"${API_KEY}\"}}}" \
"https://identity.api.rackspacecloud.com/v2.0/tokens"
)
if [[ "$result" =~ "$parse_token" ]]; then
token="${BASH_REMATCH[1]}"
echo $token
return 0
else
return 1
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment