Skip to content

Instantly share code, notes, and snippets.

@floriandejonckheere
Last active May 27, 2016 08:50
Show Gist options
  • Save floriandejonckheere/d2e27ab1de50202cabd2e44e1c9f9513 to your computer and use it in GitHub Desktop.
Save floriandejonckheere/d2e27ab1de50202cabd2e44e1c9f9513 to your computer and use it in GitHub Desktop.
Simple backup script
#!/bin/bash
# don't do anything
DRYRUN=0
# add trailing slash
BACKUPDIR="/backup/files/"
# generate final bzipped tarball, uses pbzip2
TARBALL=/backup/backup.tar.bz2
# encrypt xzipped tarball
[email protected]
## DO NOT EDIT BELOW THIS LINE ##
declare -A DIRS
DIRS[logs]="${BACKUPDIR}/logs/"
DIRS[databases]="${BACKUPDIR}/databases/"
for DIR in ${DIRS[@]}; do mkdir -v -p "${DIR}"; done
TIMESTAMP=$(date -Is)
declare -A DATES
function log() {
echo "[$(date -Is)] $@"
}
function tic() { DATES[${1:-default}]=$(date +%s); }
function toc() { echo "$(($(date +%s) - DATES[${1:-default}]))"; }
log "Started"
tic
echo $TIMESTAMP > "${BACKUPDIR}/timestamp"
## Files
log "{files} Started"
tic files
rsync -avR $((( $DRYRUN )) && printf -- -n) --delete \
/srv \
/var/lib/mysql \
/var/log \
/var/mail \
/etc \
/home \
\
"${BACKUPDIR}/root/" | gzip > "${DIRS[logs]}/${TIMESTAMP}_rsync.log.gz"
log "{files} Finished in $(toc files) seconds"
## Databases
DATABASES=(mydatabase)
log "{database} Started"
tic db
for DB in ${DATABASES[@]}; do
log "{database} Dumping ${DB}"
(( $DRYRUN )) || {
cd /
umask 277
(sudo -u postgres -H pg_dump "${DB}") > "${DIRS[databases]}/${DB}.sql"
}
done
log "{database} Finished in $(toc db) seconds"
if [[ $TARBALL ]]; then
log "{tarball} Started"
tic tarball
tar cf "${TARBALL}" "${BACKUPDIR}" --use-compress-program=pbzip2
log "{tarball} Compressed backup size is $(du -h ${TARBALL} | cut -f 1)"
log "{tarball} Finished in $(toc tarball) seconds"
if [[ $GPG_RECIPIENT ]]; then
log "{encrypt} Started"
tic encrypt
gpg --encrypt --recipient "${GPG_RECIPIENT}" "${TARBALL}"
log "{encrypt} Finished in $(toc encrypt) seconds"
rm "${TARBALL}"
fi
fi
log "Finished in $(toc) seconds"
log "Backup size is $(du -sh ${BACKUPDIR} | cut -f 1)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment