Backup your server files and databases through the command line

Number one priority when running a server!

As everybody knows (or should know), taking backups has the highest priority when running (production) servers. They can save your life in an infinite amount of cases: corruption of data files, failed upgrades, accidental removal of files,… Even in some rare cases when you are shut down by your host (due to resource usage, breaking TOS rules,…) it can help you get your website back online as soon as possible.

If you have a control panel installed it is very likely that there is some built in function to create a backup. This can be helpful if you plan to restore backups using the same control panel in the future. I prefer to manually create my backups through the command line. Why? Because it is easy, you have control on what is happening and you can do it from almost every computer or smart device with command line access.

This article will show you what commands to use to backup files on your server as well as databases (MySQL, MariaDB,…).

→ A Linux (Ubuntu) server that has gone through the initial setup.
→ Know how to connect to your server via SSH (Terminal, Putty,…) or via a client that supports SFTP.
Backup Files

The command to backup files is the following:

tar -cvpzf FILENAME.tar.gz /DIRECTORY

Here is a detailed explanation of each part of the command to better understand why it is there and what it does:

  • tar: The term tar” is derived from “tape archive”. The tar command is used to store and extract files from a tape or disk archive.
  • : Each letter after “-” stands for a specific option. So we will always define the options starting with “-“.
  • c: Stands for “create” and this tells the command to create a new archive.
  • v: This will tell the command to operate “verbosely”. In computing, this refers to a mode or setting that displays or gets extended information. So you will see the progress when the backup is being created.
  • p: Specifying this option instructs tar that it should use the permissions directly from the archive and thus “preserve permissions”.
  • z: Adding this will “zip” the file. It tells tar to read or write archives through gzip. This also means that we will need to add the extension “.tar.gz” to our filename instead of simply “.tar”. Doing this will decrease the size of the archive.
  • f: This will simply use archive file (or device) ARCHIVE.
  • FILENAME.tar.gz: This is obviously the filename for the archive. You can choose this name. I usually use a string (domain name for example) and add the date with this format: YYYY_MM_DD. This will make it easier to find the correct backup if you need it.
  • /DIRECTORY: This is the directory you would like to backup. You can use “/” to backup your complete server, but this will take quite a while. Most of the time I simply backup the directory “/var/www” as this contains the files for my websites.

The backup should appear in the /root directory. I try to backup up my websites (/var/www) at least once a week and backup the complete server once a month. This highly depends on how much you change server/website files though. I usually download the archive afterwards. This can be done via the command line, but I prefer to do this with a regular FTP application. To restore the backup you can simple upload the files manually or upload the archive and use tar again to extract it.

There a lot more options for the tar command, you can check all of them out at ComputerHope. They have an extensive and detailed list for every option. You can of course always check the manual through the command line using the man command (e.g. man tar). The command I have provided should suffice for most of the people though.

Backup Database

Wether you use MySQL, MariaDB or another RDBMS you can use the following command to backup your databases.

mysqldump -u DBUSER -pDBPASS DBNAME | gzip > FILENAME.sql.gz

While the tar command has several options, mysqldump is a bit easier if you wish to simply backup your complete database. There are of course options that you can configure. Use man mysqldump to check them out.

  • mysqldump: The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.
  • : Once again, we use “-” to define an option.
  • u DBUSER: This tells the command that the username of the database we wish to backup is following, “DBUSER”.
  • -pDBPASS: This option indicates that a password is necessary to access the database. You can type your password directly after “-p” with now spaced or you can leave it blank. When left blank you will be prompted for the password afterwards.
  • DBNAME: You can simply fill in the name of the database here.
  • gzip: We will once again gzip the backup of the database to significantly decrease the file size.
  • FILENAME.sql.gz: Choose a filename for your backup. If you do not gzip your file, simply remove “.gz” from the extension. I usually use the database name + the date with this format: YYYY_MM_DD.

The backup should appear in the /root directory. I try to backup my databases at least once every few days or at least when I have made a lot of changes. Always backup your database when you are doing WordPress upgrades for example. If something goes wrong during the upgrade you can easily restore the backup and it will save you hours or even days from trying to fix the issue.

Restore Database with backup

Wether you use MySQL, MariaDB or another RDBMS you can use the following command to restore your database from an available backup. Make sure that the backup is uploaded correctly in the /root directory and is unzipped (.sql)!


As you can see, it is a requirement that the database is already created and that a user is linked with it. Check out this guide on how to install MariaDB and create a database for more information. This will populate the existing database with the tables from the backup.


As you can see, the commands to backup your server are really simple. Store them somewhere or remember them and remember to take a backup once in a while! If you want, you can even set up a cron job to automate everything. Like I have also mentioned, don’t forget to store the backups offline on your computer or personal hard drive once in a while. This can be very useful in the case that your server crashes and your host has no backup available. Good luck!


  1. please help solve this 502 bad gateway for me. thanks

  2. please help abolish this so called 502 bad gateway. thanks

Leave a Comment.