Rsync备份VPS

服务器

备份mysql

vim backup.sh

#!/bin/sh
mysqldump -h '172.18.0.3' -u wikiuser -p'mysecret' --databases wikidb > ~/mediawiki/dbbackup/wikidb.`date +%y%m%d`.sql

chmod 755 backup.sh

每日

crontab -e

0 6 * * 1 sh ~/mediawiki/backup.sh

Rsync

$ echo "pid file = /var/run/rsyncd.pid
port = 873
uid = root
gid = root
use chroot = no
read only = yes
hosts allow=*
hosts deny=*
max connections = 5
log file = /var/log/rsync.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[rsync]
path = /需要同步的路径/
list=yes
ignore errors
auth users = root
secrets file = /etc/rsyncd/rsyncd.secrets
comment = data
" > /etc/rsyncd/rsyncd.conf



$ echo "root:password" > /etc/rsyncd/rsyncd.secrets
$ chmod 600 /etc/rsyncd/rsyncd.secrets

客户端

vim rsync.sh

#!/bin/sh

# kdaye
echo "rsync from kdaye"
rsync -az root@xxx.xIP.37.xxx::rsync --password-file=/data/backup/rsync.password /data/backup/kdaye/
echo "password" > /data/backup/rsync.password
chmod 600 rsync.password
chmod 755 rsync.sh

每日

由于archlinux base自带了timer,所以我就用timer来执行每日计划
vim /etc/systemd/system/backup-work.service

[Unit]
Description=daily backup work
RefuseManualStart=no
RefuseManualStop=yes

[Service]
Type=oneshot
ExecStart=/data/backup/rsync.sh

vim /etc/systemd/system/backup-work.timer

[Unit]
Description=daily backup work
RefuseManualStart=no
RefuseManualStop=no

[Timer]
Persistent=false
OnCalendar=Mon-Fri *-*-* 09:10:00
Unit=backup-work.service

[Install]
WantedBy=default.target
systemctl daemon-reload
# 测试下这个服务能不能用..
systemctl start backup-work.service
# 开启并自启动
systemctl start backup-work.timer
systemctl enable backup-work.timer
Show Comments