Website Backup Shell Script
January 15th, 2020 / By Andrew Currie
January 15th, 2020 / By Andrew Currie
The GitHub Gist below can be used to easily backup your PHP/MySQL websites. All you have to do is connect to your webserver over SSH, determine where you want to store the backup script, copy the contents of the Gist below to a new file named backup.sh or whatever name you like just make sure it ends with .sh. Before you save the file, go through it line by line and follow the included instructions.
You are going to need to define your website’s document root and your MySQL database name and your MySQL username and password to connect to said database. Once you have done all that save the file then don’t forget to issue a chmod +x backup.sh command (or whatever you called your file) to make it executable. After that you are ready to easily backup your website!
To run the backup script just type ./backup.sh and press enter while inside the directory where your script is stored. When it’s all said and done you’ll have one TAR archived backup labeled Website-Backup-(YEAR)-(MONTH)-(DAY).tgz. The year month and day are all the current month, day and year and the script is verbose so you will be notified of the backup status as it happens.
Once you extract the backup archive using tar -xvzf you will find your website’s entire document root compressed into a single archive labeled webroot.tar and your MySQL database will be stored in the database.sql MySQL file. I hope someone finds this script useful and if you have any tips or suggestions on how to improve it please feel free to add some comments. I plan on posting an update to this post sometime soon explaining how to completely automate your website backups with this script using a cronjob task. Thanks for reading and long live backups!