Skip to content

Instantly share code, notes, and snippets.

@r14c
Last active October 5, 2015 18:28
Show Gist options
  • Save r14c/2dd3c56011c08d395975 to your computer and use it in GitHub Desktop.
Save r14c/2dd3c56011c08d395975 to your computer and use it in GitHub Desktop.
cloud-utils

cloud-utils

A few command line utilities for various tasks.

Installation

$ curl -L https://gist.github.com/xj9/2dd3c56011c08d395975/raw/9ba87d40893e98e7b2d0d9f123a4be60664489c0/install.sh | sh
#!/bin/bash
#
# add-cron-task
#
# SYNOPSIS
#
# add-cron-task TASK
#
# DESCRIPTION
#
# Adds the given cron task to the current user's crontab. Duplicate tasks
# are not added.
cmd="$1"
exec (crontab -l ; echo "$cmd") 2>&1 | grep -v "no crontab" | sort | uniq | crontab -
#!/bin/bash
#
# backup
#
# SYNOPSIS
#
# backup APP LABEL
#
# DESCRIPTION
#
# Reads from stdin, compresses it, and streams the result to Manta. All
# subprocesses are run at the lowest possible priority.
#
# ENVIRONMENT
#
# Requires the following environment variables:
#
# MANTA_USER
# MANTA_SUBUSER
# MANTA_URL
# MANTA_KEY_ID
#
# If the upload is larger than 5GB you must specify an estimate using
# the environment variable `CONTENT_LENGTH`.
app="$1"
label="$2"
manta_path="/$MANTA_USER/stor/backup/$(date --iso-8601 | sed s,-,/,g)"
dump="$manta_path/$app-$label.gz"
>&2 echo "backup: saving backup to '$dump'.."
if [ -z ${CONTENT_LENGTH+x} ]; then
exec nice -n 19 cat /dev/stdin \
| nice -n 19 gzip -9 \
| nice -n 19 mput --parents \
--role-tag=backup \
"$dump"
else
exec nice -n 19 cat /dev/stdin \
| nice -n 19 gzip -9 \
| nice -n 19 mput --parents \
--role-tag=backup \
--header "Content-Length: $CONTENT_LENGTH" \
"$dump"
fi
#!/bin/sh
# Installs cloud-utils for the current user.
bin_path="$HOME/.local/bin"
urls=("https://gist.github.com/xj9/2dd3c56011c08d395975/raw/2d5fc1f1c63e5441629cb701d609d05b892576a6/backup"
"https://gist.github.com/xj9/2dd3c56011c08d395975/raw/1b3eefadd3a8db804a1df93d325d44120d4ce978/snapshot"
"https://gist.github.com/xj9/2dd3c56011c08d395975/raw/1b3eefadd3a8db804a1df93d325d44120d4ce978/add-cron-task"
"https://gist.github.com/xj9/2dd3c56011c08d395975/raw/1b3eefadd3a8db804a1df93d325d44120d4ce978/remove-cron-task")
>&2 echo "install: creating '$bin_path'.."
mkdir -p "$bin_path"
for url in ${urls[@]}; do
name=`basename $url`
>&2 echo "install: downloading '$name'.."
curl -sL "$url" > "$bin_path/$name"
>&2 echo "install: setting exec bits.."
chmod +x "$bin_path/$name"
done
Copyright (c) 2015 Hero Punch LLC
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.
#!/bin/bash
#
# remove-cron-task
#
# SYNOPSIS
#
# remove-cron-task TASK
#
# DESCRIPTION
#
# Removes the given cron task from the current user's crontab.
cmd="$1"
exec (crontab -l ; echo "$cmd") 2>&1 | grep -v "no crontab" | grep -v "$cmd" | sort | uniq | crontab -
#!/bin/bash
#
# snapshot
#
# SYNOPSIS
#
# snapshot [mysql USERNAME PASSWORD DATABASE | postgresql DATABASE ]
#
# DESCRIPTION
#
# Dumps the specified database to stdout
function postgres_dump {
# $1: Database name
local database="$1"
>&2 echo "snapshot: PASSWORD is ignored for this database type\!"
>&2 echo "snapshot: Add the correct connection parameters to '~/.pgpass'"
nice -n 19 pg_dump -w -Fc $database
}
function mysql_dump {
# $1: Database username
# $2: Database password
# $3: Database name
local username="$1"
local password="$2"
local database="$3"
nice -n 19 mysqldump -h localhost -u $username --password=$password $database --default-character-set=utf8
}
database_type="$1"
case $database_type in
"postgresql") postgres_dump "$2";;
"mysql") mysql_dump "$2" "$3" "$4";;
*) >&2 echo "snapshot: invalid database type: $database_type";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment