HOWTO backup with find and tar

Linux command line black with white letters

Short and simple way to backup an entire directory and a mysql database. Log some informations and send an email when the job is done. This bash script was write to be executed on OSX Yosemite, but with some minor changes it should run on almost any X driven system. If you need compression just add -z (gzip) to tar command, and name your tarball [FILENAME].tar.gz

 


#!/bin/bash
# By Alexander Stocker
TODAY=$(date +%Y%m%d)
TIME=$(date +%H%M)
BKUPDIR=/your/backup/target/dir
LOGFILE="/var/log/backitup.log"

# Source directory
SOURCE=/your/data/source/you/wanna/backup
# Target tarball
TARGET="$BKUPDIR/backup_$TODAY.$TIME.tar"

function log
{
NOW=`date +"%Y.%m.%d %H:%M:%S"`
echo "${NOW} : $@"
}

# Remove old backup if needed
sudo rm $BKUPDIR/backup_*.tar
# Remove old text file containing absolute path to files
sudo rm $BKUPDIR/filestobackup.txt
# Find files to backup and write path to text file
find $SOURCE -print0 > $BKUPDIR/filestobackup.txt
tar -cf $TARGET --files-from $BKUPDIR/filestobackup.txt
log "Tarball created"

# Backup Database
sudo /usr/local/mysql/bin/mysqldump --user=USERNAME --password=PASSWORD DATABASENAME | gzip -c | cat > $BKUPDIR/DATABASENAME.sql.gz
log "Database dump created and zipped"
log "Backup done"
mail -s "Backup Log" youremail@example.com < $LOGFILE

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*