Skip to content

Instantly share code, notes, and snippets.

@datfaf
Created September 10, 2011 21:38
Show Gist options
  • Save datfaf/1208827 to your computer and use it in GitHub Desktop.
Save datfaf/1208827 to your computer and use it in GitHub Desktop.
#!/bin/bash
BACKUP_DIR="/var/db/backups"
PGUSER="postgres"
IGNORE_DATABASES="
postgres
template0
template1
"
DATABASES=`echo "\l" | psql | tail -n +4 | head -n -2 | cut -d '|' -f 1 | cut -d ':' -f 1 | sed -e 's/ //'`
time=`date +%Y_%m_%d_%k_%M`
for DB in $DATABASES; do
dobackup=1
for I in $IGNORE_DATABASES; do
if [[ $DB == $I ]]; then
dobackup=0
break
fi
done
if [[ $dobackup == 1 ]]; then
mkdir -p $BACKUP_DIR/$DB
/usr/bin/pg_dump $DB -U $PGUSER | gzip > $BACKUP_DIR/$DB/${DB}_$time.sql.gz
fi
done
-- LOCALLY --
> ssh-keygen -t rsa -C "katylava@gmail.com"
# -t is type (ie. rsa|dsa), -C is an optional comment
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/kyl/.ssh/id_rsa): /Users/kyl/.ssh/katylavagmailcom
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/kyl/.ssh/katylavagmailcom.
Your public key has been saved in /Users/kyl/.ssh/katylavagmailcom.pub.
The key fingerprint is:
ba:68:c6:19:9a:23:4c:e2:89:1a:b3:e6:75:e5:f6:63 katylava@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| |
| .S |
|.. . o. |
|O .= +.o |
|oO= *....E |
|*o +. . ... |
+-----------------+
> ssh-add /Users/kyl/.ssh/katylavagmailcom
Enter passphrase for /Users/kyl/.ssh/katylavagmailcom:
Identity added: /Users/kyl/.ssh/katylavagmailcom (/Users/kyl/.ssh/katylavagmailcom)
# you can see the keys with `ssh-add -L`
# you have to use the full path to the key
> rsync ~/.ssh/katylavagmailcom.pub thomasjefferson.katylavallee.com:~/.ssh/
> echo "
Host thomasjefferson.katylavallee.com
IdentityFile ~/.ssh/katylavagmailcom
" >> ~/.ssh/config
-- REMOTE --
> cd ~/.ssh
> cat katylavagmailcom >> authorized_keys
> chmod 600 authorized_keys # if it didn't exist already
#!/bin/bash
usage() {
echo "${1}
Usage:
gist-this.sh <-:filename> <-:repo> <content>
echo <content> | gist-this.sh <-:filename> <-:repo>
(use - for random file name)
Required Settings:
GIST_DIR (required) = directory under which gist repos live
DEFAULT_GIST_REPO = default repo to paste to if not specified (i.e. your public repo)
GIST_URLS_INI = path to ini file that maps local gist directory names to gist.github.com repo names.
if this file or setting does not exists, the script assumes directories have same
names as repos on github.
Gists:
"
[ -d "${GIST_DIR}" ] && ls -R "${GIST_DIR}" || echo "GIST_DIR not defined"
exit 1
}
# validate
[ ! -d "${GIST_DIR}" -o -z "${DEFAULT_GIST_REPO}" ] && usage "GIST_DIR and DEFAULT_GIST_REPO must be defined"
[ -z "${1}" -o -z "${2}" ] && usage "Missing arguments"
# set vars
filename=
[[ '-' == "${1}" ]] && filename=`openssl rand -base64 128 | md5` || filename="${1}"
repo=
[[ '-' == "${2}" ]] && repo=$DEFAULT_GIST_REPO || repo=$2
gist_dir="${GIST_DIR}/${repo}"
[ ! -d ${gist_dir} ] && usage "Directory ${gist_dir} does not exist."
repo_url=
[ -z ${GIST_URLS_INI} -o ! -f ${GIST_URLS_INI} ] && repo_url=$repo || repo_url=`awk -v repo=$repo -F = '$1 == repo {print $2}' ${GIST_URLS_INI}`
gisturl="https://gist.github.com/$repo_url#file_${filename//[^a-zA-Z0-9\.]/_}"
content=
[[ -z "${3}" ]] && content= || content="${3}"
# if content is empty, read from pipe
[[ -z "${content}" ]] && {
tmpfile=/tmp/gist-this-"${filename}"
:>"${tmpfile}"
while IFS='' read -r data; do
printf "%s\n" "${data}" >> $tmpfile
done
content=`cat $tmpfile`
# if content is still empty, exit
[[ -z "${content}" ]] && usage "No content provided"
}
# redirect piped input to FD 6 and close so we can accept interactive input
exec 6<&0 6<&-
exec 0</dev/tty
# ask what to do if file exists
[[ -f "${gist_dir}/${filename}" ]] && {
echo 'File already exists -- [o]verwrite, [a]ppend, or [c]ancel?'
read dowhat
case $dowhat in
o) echo 'Overwriting'; :>"${gist_dir}/${filename}";;
a) echo 'Appending';;
c) echo 'Cancelling'; exit;;
*) echo 'Not an option. Cancelling.'; exit;;
esac
}
# create/update file
pushd `pwd` > /dev/null
cd "${gist_dir}"
echo "${content}" >> "${filename}"
# commit
git add "${filename}" &> /dev/null
git commit -m "New gist ${filename}" &> /dev/null
git push &> /dev/null
# output url
echo "$gisturl"
popd > /dev/null
GIT(1) Git Manual GIT(1)
NAME
git - the stupid content tracker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment