20 Apr How to Make Site Backups
If you’re human like most of us, then you have probably had the unfortunate experience of losing hours (or even days) worth of work due to an accidental coding mistake or mis-typed command. When this happens, the end result is spending even more time trying to duplicate the work that was lost. Wouldn’t it be nice if there was an “easy” button that could be used to instantly revert such a mistake? This article will explore how you can create your very own “easy” button by manually backing up your site which will allow for instant rollbacks when needed.
If you have a control panel, such as cPanel, there is normally a utility that will create backups for you. The Installatron plugin for cPanel will even let you automate this process! Even with the niceties provided by the control panel interface, it is still not a bad idea to learn how to create backups manually for your own peace of mind.
When creating a backup of a website, all you’re really doing is creating an export “dump” of your database and then packaging that dump together with a copy of the website’s files. To understand how this works, lets explore the process via SSH.
First, log into your account via SSH and cd into your website’s root directory.
Next, you will create a backup of your website’s database in the form of a MySQL dump. For this part, you will need to know the username, password, and database name associated with your website’s database. If you’re using Magento, this information can be found in app/etc/local.xml. The command to create a MySQL dump is as follows:
mysqldump -uUsername -pPassword database_name > databaseDumpFilename.sql
This will create a file called databaseDumpFilename.sql in your current directory which contains a dump of your database. All that’s left to do now is to pack up your website’s files, along with the database dump that you just created. I prefer to use tar for this, but you can use zip or any other packaging application.
tar czvf backupFilename.tar.gz ~/public_html
The command above will (c) create a new file, (z) compress the new file with gzip, (v) output verbose information while doing this, and (f) name the file “backupFilename.tar.gz.” This will backup everything in your website’s public_html directory.
Now you will have a file in your website’s root directory that you can use to restore your account with! You can now move this file to a secure location or download it to a different computer for safekeeping.