Making site backup on Linux

Last week i had to make backup’s of some websites through the Linux shell. I usually don’t have to make server administration and i don’t remember all the linux commands that I needed to do the job, then i decided to make this post for future task guide.

When I say backup a site, i mean backup all files, even the ones that were uploaded or changed by the users, and the database.

My projects are under a version control using subversion, but there are some files that are uploaded or changed by site users and aren’t under version control. So, i need to start by cleaning the site folders removing the folder ‘.svn’ from the folders that are under version control.

First of all, i have to know how to remove a folder and all its contents. To do this i can use the “rm” command:

rm -Rf <folder name>

This command remove the folder and all its contents without asking. To achieve this behavior i set “-R” to remove recursively and “-f” to force the action and to don’t ask anything.

After know how to remove a folder, i need to find the folders to remove, this is all the “.svn”. To do this, i use the find command:

find . -follow -name “.svn” -exec rm -Rf {} \;

With the line above i search the current folder “.” and its subfolders for the folder by its name “-name ‘.svn’”, then i want to remove it ‘-exec rm -Rf {} \;’.

The “-name” is a test option for find that allow us search by the files or folders name with a pattern. This way we can search using something like “-name ‘*.jpg’“. There are several other test options that can search for files by its size, its modification date, by its permission configuration, and other. When using pattern matching it can be used with the following special chars:
*                  Matches 0 or more characters.
?                  Matches 1 character.
[AaBbCc]      Example: matches any 1 char from the list.
[^RGB]          Example: matches any 1 char not in the list.
[a-g]            Example: matches any 1 char from this range.

The “-exec” is an action option that allow the execution of other commands during the search. The command ends when the character ‘;’ is found. The “{}” means he file name that mach the condition defined by the options. The chars might have to be escaped with ‘\’ to protect them from expansion by the shell.

The find command is very complex and have many options. If you want more information check the link below.

Now i have my site folder cleaned and ready to backup. Its time to compress it and turn it in one file. To do this i ZIP the whole folder, e.q. to all its content, files and subfolders:

zip -9 -r <zip file> <folder name>

The option “-9” is used to compress more and the “-r” to do it recursively.

To finish the job i make a backup of the database (see the post Import and export a database in MySQL using Linux shell). If the export file isn’t in the sites folder it can be moved there and zipped with the other files.

mv <file path> <destination path>

Remember that the <destination path> includes the file name, even if it will be the same.

Useful links:

One thought on “Making site backup on Linux”

  1. I’m keen on this weblog considerably, Its a real nice position to read and get info.

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Code: