客户门户 聊天支持

How to organize a backup of your data to FTP storage

Implementation inexpensive and reliable backup

Backup scenarios can be very different. You can reserve only the data that is important to you (for example, directories with sites) or all the directories and files of your server and as often as you need. It should only be borne in mind that copying files that are open and used by the system may be incomplete. This is especially true for database files and temporary files.
We will talk about a fairly simple but effective backup implementation using the example of a server with the Ubuntu 20 operating system installed. The article does not pretend to cover all possible configuration options, but gives an understanding of which direction you should work in.

First of all, we will need to connect the FTP storage as a remote disk so that we can work with it as with a local disk: read, write and delete directories and files from it. To do this, we will need to install the curlftpfs package. We will also need the rsync package to copy your data. You can install both packages with the following command:

apt-get install curlftpfs rsync

Create a mount point for the remote disk:

mkdir /mnt/ftpbackup

After installing the packages, you need to set up a connection to the remote disk. Curlftpfs has many options, but we use only those that we need in this example. Options can be set as from the command line (settings will be saved until the first reboot of your server):

curlftpfs -v userXXXX:PASSWORD@FTPSERVERADDRESS /mnt/ftpbackup -o fsname=fuse -o disable_epsv -o allow_other -o umask=027 -o uid=500 -o gid=500

so specify them in the /etc/fstab file (in this case, the settings will be saved after restarting your server):

curlftpfs#FTPSERVERADDRESS /mnt/ftpbackup fuse user=userXXXX:PASSWORD,allow_other,default_permissions,umask=022,_netdev 0 0

where:
FTPSERVERADDRESS - the address of your FTP storage;
userXXXX - the name of the FTP storage user;
PASSWORD - password of the FTP storage user;
/mnt/ftpbackup - mount point where the remote disk will be connected;
umask=022 - a mask that sets the rights to directories 755.
After setting up, in the mount command output, you will see something like the following line:

curlftpfs#ftp://FTPSERVERADDRESS/ on /mnt/ftpbackup type fuse (rw,relatime,user_id=0,group_id=0,default_permissions,allow_other)

You can check the operation of a remote disk by creating, modifying, or deleting a directory or folder in the /mnt/ftpbackup directory.
If all the commands were successful, you can start setting up the backup itself. For example, it can be like this:

rsync -avz --recursive /SOURCEDATA /mnt/ftpbackup

where SOURCEDATA is the path to the directory you want to archive. The directory itself and recursively all directories and files located in it will be copied. Please note that if you have specified the root directory, then you will need a remote disk at least twice the size of your data. To check how much space your data takes up, you can use the command:

du --si --max=1 /SOURCEDATA

To save space or for more convenience, you can use several commands by specifying other directories as /SOURCEDATA. For example, if you want to reserve only the data of your sites and they are located in the /var/www folder, you can use the command:

rsync -avz --recursive /var/www /mnt/ftpbackup

The above commands can be executed from the command line and check the results of their execution.
In order for backup to be performed without your participation, you need to create a cron job. To do this, go to cron with the crontab -e command and add a line with a task, for example, like this:

0 7 * * * rsync -T=/tmp -avz /SOURCEDATA /mnt/ftpbackup

After saving the task every day at 7 a.m., the /SOURCEDATA directory will be copied to the remote disk.

In the future, we will add a few words to the article about how to rotate backups.

19-10-2021, 09:41