The server

  • 192.168.0.248: local server
  • 47.56.34.2: Remote server

purpose

Synchronize remote server data to local server

The premise

Rsync and CronTabs are installed on both servers

yum install -y rsync
yum install -y crontabs
Copy the code

Set up security free login

Execute on the local server

# to generate the key
ssh-keygen -t rsa
Copy to remote server
ssh-copy-id -i /root/.ssh/id_rsa.pub  "- p22 [email protected]"
# Secure loginSSH -p 22 at [email protected]Copy the code

Run commands to synchronize files

rsync -vzrtopg --progress --delete -e 'ssh -p 22'[email protected]: / root/fileUpload/home/backup/fileUpload /# output log
rsync -vzrtopg --progress --delete -e 'ssh -p 22'[email protected]: / root/fileUpload/home/backup/fileUpload / 1 > / home/logs/rsync log 2 > &1Copy the code

Crond is used for timing synchronization

# On the local server, timed synchronization
0 2 * * * root /home/backup/script/auto_rsync.sh
On the remote server, periodically back up the database
0 2 * * * root /root/backup/script/auto_mysqldump.sh
Copy the code

Synchronization script

Backup database script auto_mysqldump.sh

#! /bin/bash
DATE="$(date +%F)"
backup_dir=/root/backup/database
mysqldump -uroot -pAsdf@123 pms > ${backup_dir}/pms-${DATE}.sql
​
​
# Keep only the first 100
lPkgList=($(ls "${backup_dir}" | sort -nr))
for ((j=100; j<${#lPkgList[@]}; ++j)); do
        rm -rf "${backup_dir}/${lPkgList[$j]}"
done
Copy the code

Backup file script auto_rsync.sh

#! /bin/bash
rsync -vzrtopg --progress --delete -e 'ssh -p 22'[email protected]: / root/fileUpload/home/backup/fileUpload / 1 > / home/logs/rsync log 2 > &1 rsync - vzrtopg - the progress --delete-e 'ssh -p 22'[email protected]: / root/backup/database/home/backup/database / 1 > / home/logs/rsync - database. The log 2 > &1Copy the code

Detailed usage of the command

man.linuxde.net/rsync

-v, --verbose Verbose mode output. -q, --quiet Specifies the thin output mode. -c, --checksum Turns on the checksum switch to forcibly verify file transfer.-a, --archive Archive mode, which transfers files recursively and keeps all file attributes, is equal to -rlptGod. -r, --recursive processes subdirectories in recursive mode. -r, --relative Uses relative path information. -b, --backup creates a backup, that is, if the same filename already exists for the destination, rename the old file to ~filename. You can use the --suffix option to specify different backup file prefixes. --backup-dir Stores backup files, such as ~filename, in a directory. -suffix= suffix Defines the prefix of backup files. -u, --update only updates, that is, skips all files that already exist in DST and are later than the files to be backed up, not overwriting the updated files.-l--links Keep soft links. -l, --copy-links wants to treat soft links like regular files. --copy-unsafe-links copies only links that point outside the SRC path directory tree. --safe-links ignores links that point outside the SRC path directory tree. -h, --hard-links Retain hard links. -p, --perms preserves file permissions. -o, --owner Keeps the file owner information. -g, --group Keeps the file owner group information. -d, --devices Keeps device file information. -t, --times Keeps the file time. -s, -- SPARSE Special handling of sparse files to save space for DST. -n, --dry-run specifies which files will be transferred. -w, --whole-file Copies the file without incremental detection. -x, --one-file-system Do not cross file system boundaries. -b, --block-size= size Verifies the block size used by the algorithm. The default is 700 bytes.-e, --rsh=commandRSH and SSH are used for data synchronization. --rsync-path= path Specifies the path of the rsync command on the remote server. -c, --cvs-exclude Automatically ignores files using the same method as CVS. It excludes files that you do not want to transfer. Existing only updates files that already existin DST and does not back up newly created files. --delete deletes files that SRC does not have in DST. --delete-excluded also deletes files on the receiving end that are excluded by this option. --delete-after Deletes the data after the transfer ends. --ignore-errors Deletes I/O errors even when they occur. --max-delete=NUM Deletes a maximum of NUM files. Partial preserves files that have not been fully transferred for some reason to speed up subsequent re-transfers. --force Forcibly deletes a directory, even if it is not empty. -- Numeric user and group ids do not match numeric user and group ids to user and group names. --timeout=time INDICATES the IP timeout duration, in seconds. -i, --ignore-times Does not skip files that have the same time and length. --size-only When deciding whether to back up a file, only view the file size regardless of the file time. --modify-window=NUM Specifies the timestamp window used to determine whether files are at the same time. The default is 0. -t --temp-dir= dir Creates temporary files in dir. --compare-dest=DIR Also compares files in DIR to determine whether backup is needed. -p is equal to --partial. --progress Displays the backup process. -z, --compress Compresses backup files during transmission. --exclude=PATTERN Specifies file patterns that do not need to be transferred. --include=PATTERN Specifies file patterns that need to be transferred without excluding them. --exclude-from=FILE Excludes files of the specified pattern in FILE. --include-from=FILE Does not exclude FILE files that specify pattern matching. --version Displays the version information. --address binds to a specific address. --config=FILE Specifies another configuration FILE, not the default rsyncd.conf FILE. --port= port Specifies another rsync service port. --blocking- IO uses blocking IO for remote shells. -stats Indicates the transfer status of certain files. --progress Indicates the actual transmission process during transmission. --log-format= format Specifies the log file format. --password-file= file Get the password from file. --bwlimit=KBPS Limits the I/O bandwidth, KBytes per second. -h, --help Displays help information.Copy the code

The problem

The default SSH port is changed. 22 Cannot log in without key.

Vim /etc/ssh/sshd_config Delete the preceding three lines#
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
Copy the code

Service SSHD restart Generates a new secret key and uploads the public key to another server.

CentOS between rsync to do file incremental (backup) synchronization