How To Backup Nginx Config And Settings Files Of A Remote Linux Server to Local

Ashok Raja T
Technology Specialist
August 15, 2021
Rate this article
Views    10513

There are multiple options available to backup applications and databases. But when it comes to backup of Nginx settings and configurations, we may have to rely on traditional backup approaches. In this article, let us see how to perform a backup of Nginx Config and settings files of a remote server to a local machine.

Backup Options

The easiest way to take a backup of files from a remote server to local machine is to use scp or rsync. Working with scp is a bit easier when compared to rsync. But, one of the biggest advantages of using rsync is its synchronizing capability. If the source and target are the same, it would pull only the changes to the target. With this approach, it would be much faster than scp.

To learn more about scp and its common configuration options. checkout my earlier article on scp here.

File Copy With rsync

The basic command to copy/synchronize files and folders with rsync is

# rsync [options] [source folder] [destination folder]
$ rsync -azv /source/folder/ /my/dest/folder

In the above command, according to man pages, -a is the shortcut for -rlptgoD. Let see what does it mean.

a => archive, eqivalent to -rlptgoD
r => recursive file and folder copy
l => copy symlinks
p => preserve permissions
t => preserve modification time
g => preserver group
o => retains ownership
D => copies special files and block devices

z => compress content while transfer
v => to show detailed information on file transfer

The option “azv” is sufficient for copying files or folders within a machine. But when we try to sync content from a remote server, we may have to pass the credentials of the remote server. The below command covers most of the ideal cases. Asper the below command, ssh in the remote server is exposed at port 33 (instead of 22) and the authentication to that server is happening via ssh keys.

rsync -azv -e "ssh -p 33 -i ~/.ssh/srv_id_rsa" user@remote-server:/source/folder/ /my/folder/in-local-machine

Important points to note in the above command are argument “-e” and remote folder path. The credentials and the shell commands to the remote server are passed as double /single-quoted string for the argument “-e“. If there is a “/” at the end of the source folder, only the content of the source folder will be copied, else the source folder would be copied as a child into the destination folder.

Although we talk about backup of Nginx settings and configuration, this approach can be applied for any file sync operation.

Backup Nginx Config And Settings

The command that we saw before enables us to copy the entire folder and its content. But when it comes to Nginx, certain folders are standard installation files that don’t require a backup. The below is a screenshot of my customized Nginx environment in which I require only certain folders to be synchronized with my local folder.

nginx config

To back up specific files and folders, a text file containing the list of files and folders that are required to be synced can be passed as an input to --files-from parm of rsync command.

rsync -azv --files-from="[file_path]/nginx_files.txt" -e "ssh -p 33 -i ~/.ssh/srv_id_rsa" user@remote-server:/source/folder/ /my/folder/in-local-machine

Content of nginx_files.txt

The below is the content of nginx_files.txt to be considered as an inclusion list.

nginx.conf
/sites-available/
/sites-enabled/
/snippets/
/headers.d/
/bots.d/
/deny.d/
/conf.d/
Files inside header.d, bots.d, deny.d, conf.d are custom configuration files that are updated frequently to manage bot attacks. These files are not part of standard Nginx installation.
To exclude a list of files from being copied, a text file similar to inclusion list can be created and passed as param --exclude-from="file_name.txt" to rsync.

Connect With Password And Copy With Different Account

The below is the output from a standard Nginx installation. In this approach, we are connecting to the remote server with a password instead of ssh keys.
rsync_samplee
In the above example, connection to the server is performed with a user account “ar” but the copy operation is performed with the root account of the source machine.

Subscribe To Our Newsletter
Loading

Leave a comment