I have 3 ubuntu servers, work_svr1 and work_svr2 are work servers running on the public network, 1 is running webapp for work, and 2 is a backup server. In addition, there is a personally rented remote server as a blog server. I hope that the data on the work server 1 and the personal blog server data can be backed up to the 2 server regularly every day.
The rsync command is a remote data synchronization tool that can quickly synchronize files between multiple hosts via LAN/WAN. rsync uses the so-called "rsync algorithm" to synchronize the files between the local and remote hosts. This algorithm only transfers different parts of the two files instead of sending the entire file each time, so the speed is quite fast. rsync is a very powerful tool, and its commands also have many features and options. I used 3 of them. The detailed instructions for using the options can be viewed using the man command.
# Copy local files. When neither SRC nor DES path information contains a single colon":"This working mode is activated when the separator is used.
rsync [OPTION]... SRC DEST
# Use a remote shell program(Such as rsh, ssh)To realize the copy of the contents of the local machine to the remote machine. When the DST path address contains a single colon":"This mode is activated when the delimiter.
rsync [OPTION]... SRC [USER@]host:DEST
# Use a remote shell program(Such as rsh, ssh)To achieve copying the contents of the remote machine to the local machine. When the SRC address path contains a single colon":"This mode is activated when the delimiter.
rsync [OPTION]...[USER@]HOST:SRC DEST
- a,--archive Archive mode, which means to transfer files recursively and keep all file attributes.
- z,--compress compresses the backed up files during transmission.
- P is equivalent to--partial. Keep those files that have not been completely transferred for some reason, so as to speed up the subsequent re-transfer.
SSH password-free automatic login, so that you do not need to enter the ssh login password during automatic scheduled remote backup.
First, generate a public key for user a
on the client server A
(passphrase does not need to be entered):
a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key(/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase(empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in/home/a/.ssh/id_rsa.
Your public key has been saved in/home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3 e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
Next, log in to B
with ssh as user b
and create a ~/.ssh
directory (it is possible that this directory already exists):
a@A:~> ssh b@B mkdir -p .ssh
b@B's password:
Finally, append the public key of the client A
to the authorized_keys
file on the B
end:
a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:
Now you can log in to B
without entering a password:
a@A:~> ssh b@B
# Work on the server_Script program for remote backup of my blog running on svr2 (blog is based on hexo, highly recommended^_^)
rsync -azP [email protected]:/home/user/myblog /home/user/myblogbak > rsync.log
# webapp content
rsync -azP user@work_svr1:/var/www/webapp /home/user/> rsync.log
# Backup personal git project
rsync -azP user@work_svr1:/home/user/prj_git /home/user/> rsync.log
Finally, set the crontab -e
timing task, ok.
This article partly refers to the content of this rsync
and this ssh
.
Recommended Posts