Attention
The hard drive/partition must exist on the system and be physically connected.
Attention
You cannot use the same mount point for files and the database.
If the disk has the necessary partitions, go to step 2.
If there are no partitions, we mark the disk any markup program, for example like this:
sudo cfdisk /dev/sdX
and create a file system on it with the command:
sudo mkfs.ext4 /dev/sdX
where Х is the end of the drive name.
We stop services with the commands:
sudo service postgresql stop
sudo ibatyr stop
Edit the file /etc/fstab:
sudo nano -w /etc/fstab
We write the line in it:
/dev/sdX /var/lib/postgresql/ ext4 rw,noatime 0 2
where /dev/sdX is your hard drive; /var/lib/postgresql/ — directory in which the contents of the disk will be displayed; ext4 - file system type (if you use another file system jfs/xfs/reiser, etc., this option changes), rw - permission to read-write to disk. You can also register the disk using UUID. You can find out UUID of a disk using the command:
sudo blkid
In this case, the first part of the entry will take the form UUID=, where you need to enter the result of the output of the above command in quotes.
Warning
Partitions with GPT formatting can only be mounted using the UUID of the disk!
Create a backup folder and move data from the database directory to it:
mkdir /home/user/rezerv && sudo mv /var/lib/postgresql/* /home/user/rezerv
where user is the user’s home directory. Most likely, it is different for you. You can find out the user’s home directory with the command:
env | grep -E "home|HOME"
The result of issuing this command will be the home directory of the user on whose behalf this command was executed.
We check the correctness of mounting with the command:
sudo mount -a
This command mounts a disk that is already registered in fstab, but not yet mounted.
If we entered something incorrectly or made a mistake, we will see mounting errors and, accordingly, will have the opportunity to correct the mistake. We check whether the disk is mounted correctly with a command like:
df -h
You can also check that this section is writable. For example, let’s create a text file and check its presence with the commands:
touch /var/lib/postgresql/15/main/test_write.txt && ls -l /var/lib/postgresql/15/main/
Or simply by executing the command mount without parameters: its result will be the output of all mounted systems; our new device should be mounted rw.
If you are using PostgreSQL 11, here and below specify the correct version in the path - for example, /var/lib/postgresql/11/main/
Copy everything to your hard drive:
sudo cp -R /home/user/rezerv/* /var/lib/postgresql/
If you moved the data to another directory, make changes in accordance with the actual location of the files.
Change the owner to postgres:
sudo chown -R postgres:postgres /var/lib/postgresql/15/main
We set his access rights to 700:
sudo chmod -R 700 /var/lib/postgresql/15/main
We launch the PostgreSQL, iBatyr Monitor and Nginx services:
sudo service postgresql start
sudo ibatyr start
We execute the command:
sudo ibatyr sql
in the invitation that appears, write analyze; We are waiting.
Errors like ПРЕДУПРЕЖДЕНИЕ: pg_shdescription пропускается — только суперпользователь может анализировать этот объект are not critical, because they indicate that the command launched with these rights was unable to analyze the database service tables. This is not required.
We go to the web interface, check that everything is working, all reports are visible, etc. If everything is in order, you can delete backup files:
rm -R /home/user/rezerv
Use rm with care to avoid losing data.