Automatically Back Up Your Web Server Every Night

First, SSH into your web server, and in your home directory, create a folder named backups. Inside this folder, create a file named backup.sh. In this example I’m using an external HD as my backup location.

Script: ~/backup.sh
Website: /var/www/html
External HD: /mnt/HD1/backups

#!/bin/sh
THESITE="smarterware.org"
THEDB="my_database_name"
THEDBUSER="my_database_user"
THEDBPW="my_database_password"
THEDATE=date +%d%m%y%H%M
mysqldump -u $THEDBUSER -p${THEDBPW} $THEDB | gzip > /mnt/HD1/backups/dbbackup_${THEDB}${THEDATE}.bak.gz
tar czf /mnt/HD1/backups/sitebackup
${THESITE}${THEDATE}.tar -C /var/www/html
gzip /mnt/HD1/backups/sitebackup
${THESITE}_${THEDATE}.tar
find /mnt/HD1/backups/site* -mtime +2 -exec rm {} ;
find /mnt/HD1/backups/db* -mtime +2 -exec rm {} ;

Edit THESITE, THEDB, THEDBUSER, and THEDBPW to reflect your own server. Site Name, Database Name, Database User, and Database Password respectively.

Line 7 is running a mysqldump of all the data in the database named in line 3, archiving it, and storing it in the backup directory using a filename that looks like dbbackup_example.com_1402120101.bak.gz.tar.

Line 8 and 9 are archiving the site’s source code files from the website (/var/www/html), and storing them in the backup directory, using a filename that looks like sitebackup_example.com_1402120101.tar.

The last lines are deleting any backups made more than 2 days ago. You can change 2 to any number/day you want.

Source: http://lifehacker.com/5885392/automatically-back-up-your-web-site-every-night

Automatically Back Up Your Web Server Every Night was originally published on Something Different

Automatically Back Up Your Web Server Every Night was originally published on Something Different

Automatically Back Up Your Web Server Every Night was originally published on Something Different

Automatically Back Up Your Web Server Every Night was originally published on Something Different

Advertisements