We always had a strong position about backups. Everyone must do backups!!! It is not optional, it is mandatory! It is a process that’s well understood, but somehow underestimated by many administrators and operators.
And you only have to obey the following rules:
- Backup all important data
- Backup on regular intervals
- Restore often to verify backup works
Yes, there are others, and you can start reading here, but stick to those above and you’ll sleep at least a bit easier.
To help you get started, here is a script we are using on our Linux boxes:
#!/bin/sh # Directories to backup B_DIRS=" " # MySQL Databases to backup B_DBS=" " # Backup ID B_ID=`date +%Y%m%d-%H%M` # Backup Destination (local directory or ssh-user@host:/path) B_DEST="" # SQL Credentials SQL_USER="" SQL_PASS="" SQL_HOST="" # Paths of different system utilities MYSQLDUMP="/usr/bin/mysqldump" RSYNC="/usr/bin/rsync" OPENSSL="/usr/bin/openssl" TAR="/bin/tar" BZIP2="/bin/bzip2" LOGGER="/usr/bin/logger" # Temporary folder TMP_DIR="/tmp" ########################################################################### # !!! DO NOT EDIT AFTER THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING !!! # ########################################################################### # Saving current working directory CWD=`pwd` # Check if all the utilities are already preset and available if [ ! -x "$LOGGER" ]; then echo "backup.sh - Missing logger utility" exit 1 fi if [ ! -x "$MYSQLDUMP" ]; then $LOGGER "backup.sh - Missing mysqldump" exit 1 fi if [ ! -x "$RSYNC" ]; then $LOGGER "backup.sh - Missing rsync" exit 1 fi if [ ! -x "$BZIP2" ]; then $LOGGER "backup.sh - Missing bzip2" exit 1 fi if [ ! -x "$TAR" ]; then $LOGGER "backup.sh - Missing tar" exit 1 fi if [ ! -x "$OPENSSL" ]; then $LOGGER "backup.sh - Missing openssl" exit 1 fi # Check if all the directories are present and writable if [ ! -d "$TMP_DIR" -o ! -w "$TMP_DIR" ]; then $LOGGER "backup.sh - Temporary directory does not exist or is not writable" exit 1 fi for B_DIR in $B_DIRS; do if [ ! -d "$B_DIR" -o ! -r "$B_DIR" ]; then $LOGGER "backup.sh - $B_DIR does not exist or is not readable" exit 1 fi done # Create temporary directory mkdir $TMP_DIR/backup-$B_ID if [ ! -d "$TMP_DIR/backup-$B_ID" ]; then $LOGGER "backup.sh - Can not create temporary directory" exit 1 fi # Copy every directory to the backup localtion for B_DIR in $B_DIRS; do cp -R $B_DIR $TMP_DIR/backup-$B_ID done # Dump all databases for DB in $B_DBS; do $MYSQLDUMP --single-transaction -h $SQL_HOST -u $SQL_USER -p"$SQL_PASS" $DB > $TMP_DIR/backup-$B_ID/$DB-`date +%Y%m%d`.sql done # Package everything $TAR -jcf $TMP_DIR/backup-$B_ID.tar.bz2 $TMP_DIR/backup-$B_ID # Encrypt backup (NOTE: If you enable this modify the next two steps appropriately) # $OSSL -in $TMP_DIR/backup-$B_ID.tar.bz2 enc -aes-256-cbc -salt -pass pass:'YOUR_PASS' -out $TMP_DIR/backup-$B_ID.tar.bz2.enc # Distribute towards destination $RSYNC $TMP_DIR/backup-$B_ID.tar.bz2 $B_DEST # Clean Up rm -rf $TMP_DIR/backup-$B_ID $TMP_DIR/backup-$B_ID.tar.bz2 # Log statistic information $LOGGER "backup.sh - Backup finished at `date +%Y%m%d-%H:%M`"
You need to edit several parts of it, but it works OK for most of our needs and costs nothing.
Edit:
- B_DIRS and B_DBS to identify what;
- B_DEST to identify where the backup should be stored;
- SQL_* to supply MySQL credentials.
Save as backup.sh and add it to your crontab.
Enjoy!