Backup my Nginx website
Hello, I need to backup the website on my Linode in one of the following formats: zip, tar.gz, tar.bz2, tgz, tar. I also need a dump (backup) of the database in one of the following formats: sql, sql.gz or sql.zip. How do I do this?
1 Reply
The first step in backing up your website running on Nginx will be locating where your website files reside. You should be able to do this by reviewing the server block configuration for your website which located by default in the /etc/nginx/conf.d/ directory. Once you have determined the name of the configuration for this website you can use the command below to determine where Nginx is looking for your website files by replacing $filename with the name of your website configuration file:
cat /etc/nginx/conf.d/$filename | grep root
This works because the path to your website files is identified next to the "root" parameter in your Nginx ServerBlock. While you're looking at this file I would recommend backing it up as well using just a simple copy command like the one below:
cp /etc/nginx/conf.d/$filename /etc/nginx/conf.d/$filename.bak
You can then move this configuration file to a new computer or VPS along with your website files so that it doesn't need to be recreated if you deploy your website elsewhere.
Once you have determine where your website files are located you can use either a zip or tar command like one of the ones listed below to create a backup of this directory using your preferred compression method:
sudo tar -czvf mywebsite.tar.gz /path/to/website/files
sudo tar -cjvf mywebsite.tar.bz2 /path/to/website/files
sudo zip -r mywebsite.zip /path/to/website/files
You can see more information on creating these files in the guide below:
Creating backups with tar and zip
At this point you will have created a backup of your website files so the next thing that we will look into is how to create a backup of your website's database. This process will vary depending on what database software you are using but you should be able to use one of the commands below to get this done. Please not that you will need to replace the variables in brackets [] with system specific variables.
sudo mysqldump -u [username] -p [databaseName] > [filename]-$(date +%F).sql
sudo mongodump --out [filename].sql
sudo su - postgres && pg_dump [dbname] > [filename].sql
You can see more information on this topic in the guides below:
Backing up MySQL or MariaDB
Backing up PostgreSQL
Backing up MongoDB
Once you have successfully created backups of your serveblock, your website files, and your database, you will want to look into moving them off of your Linode and to either a local computer or a different Linode. I would recommend using either and SCP command or an Rsync command to get these files transferred.
At this point you should have a backup of both your website files and your database file created and copied to a different computer or VPS. We could be done now but it is always best practice to verify your backups by deploying them to a new server and making sure that everything works properly. In this scenario you would need to ensure that you have a clean VPS running NGINX and whatever database software you were using on your backed up system. Next you will want to add the configuration or "serverblock" file to the /etc/nginx/conf.d/
directory, uncompress the file that you copied your website files to and place them in the appropriate directory that matches the name that they were created from and matches the location identified by the "root" parameter of your serverblock, and import your website's database to the new database software.
Once you have verified your backup and ensured that it is valid the backup process is complete.
I hope this information helps.