How to back up a website using SSH

01.08.2009 0

So now you have a website. Maybe even many websites. Just like anything else though. They can break and if there’s not a copy of the site. You may have allot of time trying to fix the problem and in the meantime, the site(s) are down.

To backup WordPress (and most of similar systems, scripts etc) all you need is to get a copy of information from database and copy of all files. Files often are in one directory, information is usually in one database.

The first thing you need to do is determine which database is being used with your website. After you determine that, you’ll need to make a backup by entering the following command:

1
$ mysqldump -p -u username -h hostname.com name_of_database > dump.sql

Don’t forget to check what is there in dump.sql after that and ensure the database contains the proper information for the actual website your backing up. This dump will usually happen at the root of your hosting plans server.

To create the backup:

1
$ cp -R directory copy_of_directory

NOTE:
“-R” is to copy recursively, “directory” is the name of folder you are copying, “copy_of_directory” is name of created copy

After this command has been successfully ran. You now have a copy of the data files of the website.

To restore the website from the backup:

1
2
3
$ mysql -p -u username -h hostname.com name_of_database < dump.sql
$ rm -R directory
$ cp -R copy_of_directory directory

The first command, will restore the database.
The second recursively deletes directory with files.
The third restores files.

You need to delete the directory, otherwise files from backup will overwrite actual files (this is ok) but will not delete any files that was in actual copy but was not in the backup (this is not ok). You usually don’t need to delete and recreate the database because dump contains commands to drop affected tables before loading information.

If you want to create a “zipped” archive and not worry about overwriting directories:

1
$ tar -c directory -j -f backup_of_directory.tbz "-c directory" sets target, "-j"

means packing with bzip2 to make file smaller and “-f backup_of_directory.tbz” sets name of backup.

If you want to remove old files and unpack backup.

1
2
$ rm -R directory
$ tar -x -j -f backup_of_directory.tbz

“-x” means “extract”, “-j” means “unpack bzip2-packed data” “-f backup_of_directory.tbz” sets name of archive to unpack.

Of course, you will need to move your freshly made archive to a safe place in case you need to retrieve it later:

1
$ mv backup.tbz ~/some/safe/place/backup.tbz

No comments

Leave a reply