Par Catageek, mardi 22 août 2006 à la maison :: Sécurité informatique :: Lien permanent
This shell script performs a backup of your MySQL databases and of your entire web site, and sends the tar.gz archive by mail. All you need is a SSH access to the Unix/Linux web server. Using Cron, you can plan scheduled backups and receive them by email weekly, monthly, or at any time you want. Keep always a backup copy of your site and MySQL databases.
Thanks to www.prendreuncafe.com where you can find the original article in french, modified for 1and1 web hosting, where Mutt is not available. I used the Perl module MIME::Lite instead to send mails in MIME format.
No need to be a Linux user to follow this guide : it is totally platform-independent. I will guide you through all the steps to install this script.
1 Prepare your server
Using a SSH client, connect to your server and check Cron :
$ crontab -l no crontab for username
If you get this message, it's OK. If you got an error, forget about performing scheduled backups. You can still install the script and execute it manually, but it won't be possible to schedule automatic backups with Cron.
Prepare your server for the installation : create 2 directories and set restrictive access rights :
$ mkdir backup script $ chmod 700 backup script
Visitors will have a "403 Access forbidden" error when attempting to visit these directories. You can also use a .ht_access file instead of chmod to deny access.
2 Download and edit the files
Dowload the files send_mail.pl, backup_mail.sh and my_crontab
and edit backup_mail.sh using a text editor (wordpad for instance, avoid notepad)
#!/bin/bash
# Don't forget to escape special characters (as spaces) with \
DIR_BLOG="<path_to_your_blog>" # exemple : public_html/dotclear, or ./ for all your site
DIR_BACKUP="<path_to_backup_directory>" # exemple : backup
SQL_HOST="<your_mysql_server>"
SQL_USER="<your_mysql_login>"
SQL_PASS="<your_mysql_password>"
SQL_BASE="<your_mysql_database>"
CURRENTDATE=$(date +%Y%m%d)
MAIL_FROM="backup@<your_domain>"
MAIL_TO="<your_email_address>"
MAIL_SUBJECT="[$CURRENTDATE] Backup of ${DIR_BLOG} and SQL database"
MAIL_MESSAGE="Please find attached the backup of ${DIR_BLOG} for $CURRENTDATE"
DBDUMP_FILENAME="sql$CURRENTDATE.sql"
echo "Exporting database..."
touch $DIR_BACKUP/$DBDUMP_FILENAME && mysqldump -h $SQL_HOST -u $SQL_USER \
--password=$SQL_PASS $SQL_BASE > $DIR_BACKUP/$DBDUMP_FILENAME
echo "Compressing export file..."
tar czf $DIR_BACKUP/$DBDUMP_FILENAME.tar.gz $DIR_BACKUP/$DBDUMP_FILENAME
echo "Creating global archive..."
tar czf $DIR_BACKUP/blog$CURRENTDATE.tar.gz $DIR_BLOG $DIR_BACKUP/$DBDUMP_FILENAME.tar.gz
echo "Mailing archive to $MAIL_TO..."
# Comment out this line if you use mutt
#echo $MAIL_MESSAGE | mutt -s $MAIL_SUBJECT -a $DIR_BACKUP/blog$CURRENTDATE.tar.gz $MAIL_TO
# Comment out these lines if you use uuencode & mail
#(echo $MAIL_MESSAGE ; uuencode -m $DIR_BACKUP/blog$CURRENTDATE.tar.gz blog$CURRENTDATE.tar.gz) \
# | mail -s $MAIL_SUBJECT $MAIL_TO
# Comment out these lines if you want to use Perl MIME::Lite (works for 1&1 host)
perl script/send_mail.pl "${MAIL_FROM}" "${MAIL_TO}" "${MAIL_SUBJECT}" "${MAIL_MESSAGE}" \
$DIR_BACKUP/blog$CURRENTDATE.tar.gz blog$CURRENTDATE.tar.gz
echo "Deleting temporary files..."
rm -f $DIR_BACKUP/$DBDUMP_FILENAME*
echo "Backup completed in $DIR_BACKUP/blog$CURRENTDATE.tar.gz"
Modify the SQL_*, DIR_* and MAIL_* variables as needed.
Edit my_crontab and schedule the backups :
For a weekly backup every tuesday at 4.54am, we should use :
54 4 * * tue script/backup_mail.sh >/dev/null 2>&1
For a daily backup at 10.08pm :
8 22 * * * script/backup_mail.sh >/dev/null 2>&1
For a backup every 5th and 20th day of each month, at 1.22am :
22 1 5,20 * * script/backup_mail.sh >/dev/null 2>&1
For a monthly backup, every 10th day of each month, at 2.43am :
43 2 10 * * script/backup_mail.sh >/dev/null 2>&1
3 Installation and Test
Upload send_mail.pl, backup_mail.sh and my_crontab on your web server, in the script/ directory.
To check that everything is OK, run your script manually, after making it executable :
$ chmod u+x script/backup_mail.sh script/send_mail.pl $ script/backup_mail.sh
Check for errors. If there are none, you should have a tar.gz archive in backup/ and receive a mail with a tar.gz attachment.
4 Register your Cron job
Register your Cron job using this command :
$ crontab script/my_crontab
Check your Cron job :
$ crontab -l 40 20 7,22 * * script/backup_mail.sh >/dev/null 2>&1
This means a backup will be performed on days 7 and 22 of each month, at 8.40 pm.
Clean the file since we don't need it anymore :
$ rm script/my_crontab
Conclusion
Be careful of the size of your backup, since mail attachments are limited to 10 Mo by many mail providers, and base64 file encoding during mail transfer increases size by about 30%. Limit your backups to 6 Mo, or create separate cron jobs for different parts of your web site, using several backup_mail.sh scripts (backup_mail1.sh, backup_mail2.sh,...).

Commentaires
1. Le lundi 24 décembre 2007 à la maison, par acteur
2. Le mardi 8 janvier 2008 à la maison, par Catageek
3. Le samedi 2 janvier 2010 à la maison, par falafil54
S'abonner au fil rss des commentaires de ce billet
Ajouter un commentaire