#!/usr/bin/env bash
####Usage
# ./vault.sh encrypt
# ./vault.sh dencrypt
# ./vault.sh encrypt /full/path/to/file.yml
######
set -euo pipefail
cd `dirname $0`
if [ -z "$PASSWORD" ]; then
read -s -p "Enter Password: " PASSWORD
fi
VAULT_FILE=vault_key
echo "${PASSWORD}" > "${VAULT_FILE}"
ACTION=decrypt
if [ "$1" != "" ]; then
ACTION="$1"
fi
FILES=(group_vars/prod/*.yml)
if [ ! -z "${2-}" ]; then
FILES=("$2")
fi
for FILE in "${FILES[@]}"
do
if [ "${ACTION}" = "encrypt" ]; then
echo "Encrypting ${FILE}"
ansible-vault encrypt "${FILE}.decrypted" --output=$FILE --vault-password-file "${VAULT_FILE}"
else
echo "Decrypting ${FILE}"
ansible-vault decrypt $FILE --output="${FILE}.decrypted" --vault-password-file "${VAULT_FILE}"
fi
done
rm -rf "${VAULT_FILE}"
Working example here
The script creates separate directory for every database and bz2 files for every table inside that database.
#!/bin/sh
#edit these
USER=""
PASSWORD=""
MYSQLDIR="/path/to/backupdir"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
echo - Dumping DB structure "$MYSQLDIR"/all.bz2
$MYSQLDUMP --user=$USER --password=$PASSWORD -d --all-databases | bzip2 > "$MYSQLDIR"/all.bz2
echo - Dumping tables for each DB
databases=`$MYSQL --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases; do
echo - Creating "$db" DB
mkdir $MYSQLDIR/$db
chmod -R 777 $MYSQLDIR/$db
for tb in `$MYSQL --user=$USER --password=$PASSWORD -N -B -e "use $db ;show tables"`
do
echo -- Creating table $tb
$MYSQLDUMP --opt --delayed-insert --insert-ignore --user=$USER --password=$PASSWORD $db $tb | bzip2 -c > $MYSQLDIR/$db/$tb.sql.bz2
done
echo
done