📜 ⬆️ ⬇️

Creating backups remotely using ssh, scp, tcl. Several ways

Backups are a necessary thing. Especially you understand this when the law of meanness works and despite many precautionary moments, information is lost, which can take a long time to restore if there are no backups.
This article is primarily for programmers, but it is also possible that administrators will get some useful information for themselves.
The task is that we have one or several servers, on which there is frequently updated information. This information needs to be periodically backed up on some one "main" server. To do this, we will write a script that we place on the main server, which will automatically connect to each server to create backups there, archive them and copy backup archives to the main server using the SCP secure copy protocol.
And so on to our scripts.

First method

This is a bash (shell) script - uses the principle of remote passwordless entry by key, using SSH (via ssh-id). How to configure such a connection, you can read in this article

Our first script:



BACKUPDIRS="/home/test1 /home/test2"
timestamp=`date "+%Y_%m_%d"`

err() { echo -e "* Error: $1"; exit 1; }

ssh $DST_LOGIN@$DST_IPADDR 'tar -zcvf '$TMPDIR$DST_IPADDR'_'$timestamp'.tar.gz '$BACKUPDIRS || err "Remote connection failed"
scp $DST_LOGIN@$DST_IPADDR:${TMPDIR}${DST_IPADDR}_${timestamp}.tar.gz ./
ssh $DST_LOGIN@$DST_IPADDR 'rm -f '$TMPDIR$DST_IPADDR'_'$timestamp'.tar.gz' || err "Remote connection failed"

This script connects via ssh to a remote server, creates a backup, overwrites it to the main server and then deletes the created backup on the remote server.

Second method

It works on this principle - remote connection to the server using the TCL language.
It can be used when for some reason it is not possible to create a password-free login (by key) using SSH.
There is already required login data not only for the remote server, but also for the main server, for the subsequent copying of the backup to the main server

Our second script:

#!/usr/bin/expect -f
set timeout 100
set SELFPASS "mypassword"
set SELFUSER "root"
set SELFIP ""
set SELFDIR "/home/backup5"

set PASS "remotepassword"
set USER "root"
set IPSERV ""
set BackupDirectories "/home/test1 /home/test2"
set TMPDIR "/home/backup5"

spawn ssh $USER@$IPSERV
expect "assword:"
send "$PASS\r"
expect "#"
send "tar -zcvf $TMPDIR/backup.tar.gz $BackupDirectories\r"
expect "#"
send "scp -r $TMPDIR/backup.tar.gz $SELFUSER@$SELFIP:$SELFDIR/\r"
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\r"
#look for the password prompt
"assword:" {
send "$SELFPASS\r"
#he expect command will now return

send "exit;\r"
expect eof

This script connects to a remote server using the tcl language, and the command (method) expect is a step-by-step method for executing an algorithm with waiting for a response from a remote server. Next, create a backup and copy it to the main server using the same scp protocol. This script requires more thorough debugging and setting up additional tcl (expect) packages on the servers. But it is still interesting in its performance. in general, shows additional features of remote work with servers.

These scripts can also be improved. Add for example the creation of database backups (done in 1 line), also so that the script creates backups from several different servers automatically (sequentially), etc. This is according to your desire. It is also worth registering the launch of this script using crown so that backups are created automatically after a certain time.

PS I wish to always have a fresh backup!

Source: https://habr.com/ru/post/101026/

All Articles