Automated Website + Database Backup Script

Automated Website + Database Backup Script

xaitmixaitmi Member
edited January 6 in Tutorials

Just thought I'd share a script I made around last April to automate backups for my sites.

I'm not a expert or anything, just a guy who likes making stuff as I learn, and made something to suit my needs.

Line 1 of the script dumps your database into a file with the date in the filename and puts it into a folder called RAW

Line 2 zips up the contents of your site and also puts it into a folder called RAW

Line 3 zips both the site and the database, moves it into a folder called PACKED and deletes the contents of RAW.

Line 4 rSyncs it to your backup server and deletes it from the local server.

Install Instructions

A. Create a file called somewhere on your server and insert the script below into the file, modify the script with the db username/pw/dbname and modify the file paths to suit your needs.

B. Give it permission ( chmod +x )

mysqldump -u **InsertDBBackupUsernameHere** -p'**InsertDBBackupPasswordHere**' **InsertDBHere** > /home/backups/raw/db_data-`date '+%y%m%d_%H%M%S'`.sql

tar -cjvf /home/backups/raw/`date '+%y%m%d_%H%M%S'`.tar.bz2 -C /home/nginx/domains/ .

tar -cjvf /home/backups/packed/backupPackage-`date '+%y%m%d_%H%M%S'`.tar.bz2 -C /home/backups/raw .

rm -rf /home/backups/raw/*

rsync -azP --remove-source-files /home/backups/packed/ 
[email protected]**InsertIPOfBackupServerHere**:/home/backups/

C. Install the cronjob on your server

30 0 * * * /username/tools/ > /username/backups.log
  1. Ensure your server can ssh into your backup server fine, I highly recommend using ssh-copy-id

  2. Create the proper paths on your backup server and you should be good to go.

If you need a suggestion for backup server, for the past year I've been using a $15/year Ramnode 80gb backup server. Their extremely reliable.


Sign In or Register to comment.