Primeiro de tudo, versão local.
Eu omiti os "--remove-source-files" no script para teste:
[root@centos7 rsync_task]# pwd
/root/rsync_task
[root@centos7 rsync_task]# ls -la backup.sh
-rw-r--r-x 1 root root 259 Feb 13 01:15 backup.sh
[root@centos7 rsync_task]# cat backup.sh
#!/bin/sh
echo "List files in src: "$(ls /mnt/backup_src)
/usr/bin/rsync --chmod=u=rwx,g=rwx,o=rwx -r -zvh --log-file=/var/log/rsync_activity.log /mnt/backup_src/* /mnt/backup_dst
echo 'date' ' Rsync - File(s) moved to NFS Share' >> /var/log/rsync_backup.log
Arquivo de serviço:
[root@centos7 rsync_task]# ls -lah /etc/systemd/system/rsync_backup.service
-rw-r--r-- 1 root root 160 Feb 13 00:52 /etc/systemd/system/rsync_backup.service
[root@centos7 rsync_task]# cat /etc/systemd/system/rsync_backup.service
[Unit]
Description= Starts the folder monitoring/logging script
Documentation= man:systemd.service
[Service]
Type=oneshot
ExecStart=/root/rsync_task/backup.sh
Pouca verificação antes de iniciar o serviço:
[root@centos7 mnt]# pwd
/mnt
[root@centos7 mnt]# ls -lad backup_*
drwxr-xr-x 2 root root 6 Feb 13 01:15 backup_dst
drwxr-xr-x 2 root root 71 Feb 13 01:12 backup_src
[root@centos7 mnt]# ls backup_src/
file1 file2 file3 file4 file5
[root@centos7 mnt]# ls backup_dst
Iniciando o serviço:
[root@centos7 yurij]# systemctl start rsync_backup.service
[root@centos7 yurij]# systemctl status rsync_backup.service
● rsync_backup.service - Starts the folder monitoring/logging script
Loaded: loaded (/etc/systemd/system/rsync_backup.service; static; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd.service
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: List files in src: file1 file2 file3 file4 file5
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: sending incremental file list
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: file1
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: file2
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: file3
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: file4
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: file5
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: sent 236 bytes received 107 bytes 686.00 bytes/sec
Feb 13 01:24:11 centos7.localdomain backup.sh[2714]: total size is 0 speedup is 0.00
Feb 13 01:24:11 centos7.localdomain systemd[1]: Started Starts the folder monitoring/logging script.
Vamos verificar os backups:
[root@centos7 mnt]# ls backup_dst
file1 file2 file3 file4 file5
Copiar para compartilhamento NFS ("debian9" é usado como servidor NFS).
Configuração do NFS-sever:
root@debian9:~/backups# pwd
/root/backups
root@debian9:~/backups# ls -ld ./
drwxrwxrwx 2 root root 4096 Feb 13 01:35 ./
root@debian9:~/backups# cat /etc/exports
/root/backups 192.168.56.0/24(rw,sync,no_subtree_check,no_root_squash,insecure)
root@debian9:~/backups# systemctl status nfs-server
● nfs-server.service - NFS server and services
Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
Active: active (exited) since Tue 2018-02-13 01:32:10 MSK; 6min ago
Process: 1631 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
Process: 1628 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
Process: 1625 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
Process: 1645 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 1643 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 1645 (code=exited, status=0/SUCCESS)
Feb 13 01:32:10 debian9 systemd[1]: Starting NFS server and services...
Feb 13 01:32:10 debian9 systemd[1]: Started NFS server and services.
Estado do primeiro host no qual fazemos backups:
[root@centos7 mnt]# pwd
/mnt
[root@centos7 mnt]# ls backup_src
file1 file2 file3 file4 file5
[root@centos7 mnt]# ls backup_dst
[root@centos7 mnt]# mount|grep nfs
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
[root@centos7 mnt]# mount -t nfs 192.168.56.4:/root/backups backup_dst/
[root@centos7 mnt]# ls backup_dst
[root@centos7 mnt]# mount|grep nfs
nfsd on /proc/fs/nfsd type nfsd (rw,relatime)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
192.168.56.4:/root/backups on /mnt/backup_dst type nfs4 (rw,relatime,vers=4.1,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.56.5,local_lock=none,addr=192.168.56.4)
Vamos começar o serviço:
[root@centos7 yurij]# systemctl start rsync_backup.service
[root@centos7 yurij]# systemctl status rsync_backup.service
● rsync_backup.service - Starts the folder monitoring/logging script
Loaded: loaded (/etc/systemd/system/rsync_backup.service; static; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd.service
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: List files in src: file1 file2 file3 file4 file5
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: sending incremental file list
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: file1
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: file2
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: file3
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: file4
Feb 13 01:43:25 centos7.localdomain backup.sh[2874]: file5
Feb 13 01:43:26 centos7.localdomain backup.sh[2874]: sent 236 bytes received 107 bytes 228.67 bytes/sec
Feb 13 01:43:26 centos7.localdomain backup.sh[2874]: total size is 0 speedup is 0.00
Feb 13 01:43:26 centos7.localdomain systemd[1]: Started Starts the folder monitoring/logging script.
[root@centos7 mnt]# ls backup_dst
file1 file2 file3 file4 file5
Verificando no servidor NFS:
root@debian9:~/backups# pwd
/root/backups
root@debian9:~/backups# ls
file1 file2 file3 file4 file5
Por fim, tente como "rsyncd", que será exibido no host "debian9":
root@debian9:~/backups# cat /etc/rsyncd.conf
log file=/var/log/rsyncd
[Backups]
comment = backups
path = /root/backups/
use chroot = yes
lock file = /var/lock/rsyncd
read only = no
write only = yes
list = yes
uid = root
gid = root
strict modes = yes
hosts allow = 192.168.56.5 #it's my 'centos7'
ignore errors = no
ignore nonreadable = yes
transfer logging = no
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
root@debian9:~/backups# systemctl status rsync
● rsync.service - fast remote file copy program daemon
Loaded: loaded (/lib/systemd/system/rsync.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-02-13 02:06:42 MSK; 33s ago
Main PID: 2327 (rsync)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/rsync.service
└─2327 /usr/bin/rsync --daemon --no-detach
Feb 13 02:06:42 debian9 systemd[1]: Stopped fast remote file copy program daemon.
Feb 13 02:06:42 debian9 systemd[1]: Started fast remote file copy program daemon.
Verifique no diretório de backup em "debian9":
root@debian9:~/backups# pwd
/root/backups
root@debian9:~/backups# ls
Faça pequenas alterações no script de backup:
[root@centos7 rsync_task]# cat backup.sh
#!/bin/sh
echo "List files in src: "$(ls /mnt/backup_src)
/usr/bin/rsync --chmod=u=rwx,g=rwx,o=rwx -r -zvh --log-file=/var/log/rsync_activity.log /mnt/backup_src/* rsync://192.168.56.4/Backups
echo 'date' ' Rsync - File(s) moved to NFS Share' >> /var/log/rsync_backup.log
Verifique nos arquivos de origem:
[root@centos7 mnt]# pwd
/mnt
[root@centos7 mnt]# ls backup_src/
file1 file2 file3 file4 file5
Vamos começar o serviço:
[root@centos7 yurij]# systemctl start rsync_backup.service
[root@centos7 yurij]# systemctl status rsync_backup.service
● rsync_backup.service - Starts the folder monitoring/logging script
Loaded: loaded (/etc/systemd/system/rsync_backup.service; static; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd.service
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: List files in src: file1 file2 file3 file4 file5
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: sending incremental file list
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: file1
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: file2
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: file3
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: file4
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: file5
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: sent 232 bytes received 103 bytes 670.00 bytes/sec
Feb 13 02:10:13 centos7.localdomain backup.sh[3021]: total size is 0 speedup is 0.00
Feb 13 02:10:13 centos7.localdomain systemd[1]: Started Starts the folder monitoring/logging script.
Verifique nos arquivos no servidor de backup (rsynd):
root@debian9:~/backups# pwd
/root/backups
root@debian9:~/backups# ls
file1 file2 file3 file4 file5
Espero que isso ajude você.