Skip to content

Instantly share code, notes, and snippets.

@rashfael
Created March 21, 2017 18:32
Show Gist options
  • Save rashfael/bf226067037e9ac1ace2c3cb7002ed5e to your computer and use it in GitHub Desktop.
Save rashfael/bf226067037e9ac1ace2c3cb7002ed5e to your computer and use it in GitHub Desktop.
backupscripts
#!/bin/bash
# Backup script to save important IsoBeef data on Hetzner's backup service.
#
# Usage:
# ./backup
#
# Written by BOFH Lyse, licensed under WTFPL.
#
# References:
# http://www.nongnu.org/rdiff-backup/examples.html
# http://wiki.rdiff-backup.org/wiki/index.php/BackupToSshfsMount
#
# ensure root runs this script
#
if [ "`whoami`" != "root" ]; then
echo 'Only root can run "backup"!' >&2
exit 1
fi
START=`date +%s`
LOG=/root/backup-`date +%Y-%m-%d-%H-%M`.log
function info {
TEXT="`date +%Y-%m-%d\ %H:%M:%S` $@"
echo "$TEXT"
echo "$TEXT" >> $LOG
}
#
# read the configuration file, it defines
# * $BACKUP_MOUNT
# * $BACKUP_CREDENTIALS
# * MYSQL_ROOT_PASSWORD
CONFIG=$HOME/.backuprc
[ -f $CONFIG ] && . $CONFIG || {
echo "Missing configuration file $CONFIG!" >&2
exit 2
}
#
# mount the backup service
#
info "Mounting SSHFS..."
/root/backup_mount.sh
#
# do the backup work
#
function backup() {
for FULL_PATH in $@; do
# create parent directory if not present (supports directories,
# files and symbolic links)
if [ -d "$FULL_PATH" ]; then
[ -d "$BACKUP_MOUNT/$FULL_PATH" ] || mkdir -p "$BACKUP_MOUNT/$FULL_PATH"
elif [ -f "$FULL_PATH" ] || [ -h "$FULL_PATH" ]; then
DIR=`dirname "$BACKUP_MOUNT/$FULL_PATH"`
[ -d "$DIR" ] || mkdir -p "$DIR"
else
info "$FULL_PATH is not a directory, file or symbolic link, skipping!"
continue
fi
info "Backing up $FULL_PATH..."
rdiff-backup --force "$FULL_PATH" "$BACKUP_MOUNT/$FULL_PATH" 2>&1 | grep -v 'Warning: hard linking not supported by filesystem at '
done
}
# create MySQL dump
MYSQL_DUMP=/root/dumps/mysql.sql
info "Creating MySQL dump $MYSQL_DUMP..."
mysqldump --all-databases --events --force --user=root --password=$MYSQL_ROOT_PASSWORD > $MYSQL_DUMP
# create LDAP dump
LDAP_DUMP=/root/dumps/ldap.ldap
info "Creating LDAP dump $LDAP_DUMP..."
/usr/sbin/slapcat > $LDAP_DUMP
# create MongoDB dump
MONGO_DUMP=/root/dumps/mongodb
info "Creating MongoDB dump $MONGO_DUMP..."
mongodump -o $MONGO_DUMP > /dev/null
# backup stuff
backup \
/etc \
/srv/mail \
/home/dgall/Maildir \
/home/jd/Maildir \
/home/lyse/Maildir \
/home/rashfael/Maildir \
/home/sr/Maildir \
/home/wf/Maildir \
/root/dumps # MySQL, LDAP, PostgreSQL & MongoDB dumps
#
# finally umount backup system
#
info "Unmounting SSHFS..."
/root/backup_umount.sh
END=`date +%s`
DELTA=$(($END - $START))
MINUTES=$(($DELTA / 60))
SECONDS=$(($DELTA % 60))
[ $SECONDS -lt 10 ] && SECONDS="0$SECONDS"
info "Finished in $MINUTES:$SECONDS minutes."
#!/bin/bash
#
# read the configuration file, it defines
# * $BACKUP_MOUNT
# * $BACKUP_CREDENTIALS
# * MYSQL_ROOT_PASSWORD
CONFIG=$HOME/.backuprc
[ -f $CONFIG ] && . $CONFIG || {
echo "Missing configuration file $CONFIG!" >&2
exit 2
}
#
# mount the backup service
#
# create mountpoint if not present, so mounting will not fail on missing
# target directory
[ -d "$BACKUP_MOUNT" ] || mkdir "$BACKUP_MOUNT"
# actually mount the backup space
sshfs "$BACKUP_CREDENTIALS" "$BACKUP_MOUNT" -o workaround=rename -o reconnect
#!/bin/bash
echo "du" | lftp -u username,password username.your-backup.de | awk -v LIMIT=100 '$2=="." {print $1/1024 " MiB used, " ((LIMIT*1024*1024)-$1)/1024 " MiB backup space remaining"}'
#!/bin/bash
#
# read the configuration file, it defines
# * $BACKUP_MOUNT
# * $BACKUP_CREDENTIALS
# * MYSQL_ROOT_PASSWORD
CONFIG=$HOME/.backuprc
[ -f $CONFIG ] && . $CONFIG || {
echo "Missing configuration file $CONFIG!" >&2
exit 2
}
#
# finally umount backup system
#
fusermount -u "$BACKUP_MOUNT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment