I'm trying to backup Wordpress files and database with a shell script. I found one online that works great and am trying to work it a bit to provide more notices and progress info as well as work on some problems I'm having.
For starters... when I run this I get a notice after Create the archive... and Append the dump to the archive...
"tar: Removing leading `/' from member names"
Does anyone know what this means, and what I can do about it? This is run in a shared hosting environment if that helps.
Also, I'm sort of new to shell scripting but how can I add some kind of progress notice after each step, or a blinking ellipsis to designate that there is an ongoing process taking place. And one other thing... would there be a good way to secure the DB credentials so they aren't just out in the open? Also, how could I set this to cycle the files every month or set time period... like 30 days?
Any help would be appreciated.
#!/bin/bash
# This script creates a compressed backup archive of the given directory and the given MySQL table. More details on implementation here: http://theme.fm
# Feel free to use this script wherever you want, however you want. We produce open source, GPLv2 licensed stuff.
# Author: Konstantin Kovshenin exclusively for Theme.fm in June, 2011
# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="mydomain.com.$NOW.tar"
BACKUP_DIR="/home/user/backups"
WWW_DIR="/home/user/mydomain.com/"
# MySQL database credentials
DB_USER="database_user"
DB_PASS="database_password"
DB_NAME="database_name"
DB_HOST="mysql.mydomain.com"
DB_FILE="mydomain.com.$NOW.sql"
# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^home/user/mydomain.com,www,'
DB_TRANSFORM='s,^home/user/backups,database,'
# Initial setup
TODAY=$(date)
echo "----------------------------------------------------
Date: $TODAY
Host: mydomain.com automated backup"
# Create the archive and the MySQL dump
echo "----------------------------------------------------"
echo "Create the archive..."
tar -cpf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
echo "Done!"
echo "Create the MySQL dump..."
mysqldump -h $DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE
echo "Done!"
# Append the dump to the archive, remove the dump and compress the whole archive.
echo "----------------------------------------------------"
echo "Append the dump to the archive..."
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
echo "Done!"
echo "Remove the dump..."
rm $BACKUP_DIR/$DB_FILE
echo "Done!"
echo "Compress the whole archive..."
gzip -9 $BACKUP_DIR/$FILE
echo "Done!"
Just means that tar removes the leading "/" because when you tar the files, it will be relative to the directory where you untar the files.
If you untar a file and the archive contained the leading "/" then you would not have control over where the file is untarred, and then you might overwrite an exiting file by accident