Use rsync to remotely backup data under Ubuntu

demand##

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.

Introduction to rsync##

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.

usage###

# 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

Options###

- 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 login##

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

My example##

Backup personal blog

# 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

Backup working server###

# 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

Use rsync to remotely backup data under Ubuntu
Summary of methods to use Evernote under Ubuntu 14.04
Use iptables under ubuntu
How to use Docker data volumes on Ubuntu 14.04
Use doxygen to generate API documentation under Ubuntu 12.04
Use sublime tex under ubuntu
Use of mediawiki under ubuntu
Use virtualbox to deploy ubuntu
Use expect to realize screen multi-window boot operation under ubuntu
How to use hanlp in ubuntu
Remotely connect to Ubuntu via XShell
How to compile ijkplayer-android under ubuntu
How to install Audacious under Ubuntu
How to install Tensorflow under ubuntu 16.04
How to use Samba server on Ubuntu 16.04
Use python3 to install third on ubuntu
Three ways to install software under Ubuntu
How to change MySQL password under Ubuntu
windows use ubuntu to start linux service
How to install vim editor under Linux (Ubuntu 18.04)
How to use Prometheus to monitor your Ubuntu 14.04 server
How to configure TensorFlow use environment in Ubuntu
How to use Nginx's map module on Ubuntu 16.04
Use the command to solve the Ubuntu projector problem:
How to install and use Docker on Ubuntu 20.04
The solution to the failure to install .deb under Ubuntu 16.04
How to use dpkg command in Ubuntu system
How to install and use Curl on Ubuntu 18.04
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
Publish the node.js project under Ubuntu to heroku
Steps to add toolkit for Matlab_Linux under ubuntu
How to use Jenkins to build automatically on Ubuntu
How to install and use PostgreSQL on Ubuntu 16.04
Add a new project to SVN under Ubuntu
Remotely connect to MySQL database in Ubuntu environment
Introduction to the use of Hanlp in ubuntu
Solution to pycharm unable to import classes under Ubuntu
How to enable Mysql remote access under Ubuntu 14.04
How to install gcc compiler faster under Ubuntu
How to install and use Docker on Ubuntu 16.04
Ubuntu use notes
ubuntu zip/unzip use
How to repackage Deb files under Debian and Ubuntu
How to create and use MongoDB backups on Ubuntu 14.04
Use Rancher to build a K8s cluster under CentOS7
Use the command line to detect the Ubuntu version method
Use Cobbler to automate batch deployment of CentOS / Ubuntu
Installation and use of GDAL in Python under Ubuntu
How to install and use MySQL Workbench on Ubuntu 18.04
How to use Putty to log in to ubuntu installed in VirtualBox
Cpp web (1) Install and use Crow service under Ubuntu
How to install two versions of OpenCV under ubuntu?
How to open https on nginx server under Ubuntu