Version 2 (modified by Daniel Strum, 10 years ago) (diff)


How can I backup my own data?

I created a very simple script to backup my files and database from the command line.

mysqldump [database-name] > ~/[domain-directory]/web/[database-name]_`date +%y%m%d`.sql
tar cvzf ~/backups/[site]_backup_`date +%y%m%d`.tgz ~/[domain-directory]/web
rm -f ~/[domain-directory]/web/[database-name]_`date +%y%m%d`.sql

Change the info inside the bracket for your own site. Save file as "". SSH into your server and create a new directory called "backups". Put the script in that directory and make it executable (chmod 755

The first line (after #!/bin/bash) dumps a dated copy of your database right into your web directory (maybe not the best practice but it gets deleted very fast). The 2nd line creates in your "backups" directory a compressed tarball of everything in the "web" directory (including the database dump). The 3rd line deletes the database dump from the "web" directory.

I would suggest first (after creating the "backups" directory) just running the 3 lines from your command line. The results should be a new dated *.tgz file in the "backups" directory. Also check that there is no *.sql file left in your "web" directory. If you have access errors doing the database dump, see here: If this works, try running the script:


After I run this I SFTP into the server and download the new backup to my local machine. I also keep the latest version on the server and delete the previous one. I do this periodically (every 2 weeks). I'm sure this could be easily improved but it works. I am looking at better options for automated back-up to cloud storage.