设为首页 收藏本站
查看: 1252|回复: 0

LinuxShell脚本之利用rsync+ssh实现Linux文件系统远程备份

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-7-18 10:55:59 | 显示全部楼层 |阅读模式
功能介绍:
该脚本用于定期(结合crontab一起使用)将本地目录通过rsync+ssh传输到远程服务器,每次执行都生成一个带有以时间命名的目录,并且当前最新版本的数据链接到一个名字叫current的符号链接上,便于查找和恢复。在数据传输完成前,会传输到临时目录下,这个临时目录被命名为“时间-incomplete”。超过10天的备份将被删除,超过10天的日志文件也将被删除。
运行原理:
脚本运行的核心就在于ssh的互信和rsync命令。利用rsync能实现压缩传输,节省传输时间。
感谢:
感谢gregrs-uk提供的初始脚本。
脚本内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# Function description:
# Backup filesystem using rsync

# Usage:
# bash backup.sh

# Birth Time:
# 2016-07-15 16:13:43.895515929 +0800

# Author:
# Open Source Software written by 'Guodong Ding <dgdenterprise@gmail.com>'
# Github: https://github.com/DingGuodong

# Others:
# crontabs -- configuration and scripts for running periodical jobs
# SHELL=/bin/bash
# PATH=/sbin:/bin:/usr/sbin:/usr/bin
# MAILTO=root
# HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
# m h  dom mon dow   command
# execute on 11:59 per sunday
# 59 11 * * */0 bash /path/to/backup.sh >/tmp/log_backup_fs_crontab_$(date +"\%Y\%m\%d\%H\%M\%S").log
# or
# execute on 23:59 per day
# 59 23 * * * bash /path/to/backup.sh >/tmp/log_backup_fs_crontab_$(date +"\%Y\%m\%d\%H\%M\%S").log

USER="`id -un`"
LOGNAME="$USER"
if [ $UID -ne 0 ]; then
    echo "WARNING: Running as a non-root user, \"$LOGNAME\". Functionality may be unavailable. Only root can use some commands or options"
fi

old_PATH=$PATH
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

# Snapshot backup script
# Refer: https://github.com/gregrs-uk/snapshot-backup/

# directories to backup, separated by spaces
datadir_to_backup="/data/docker"
# backup location on remote server
# This path should not contain spaces, even if they are escaped
remote_destination="/data/backup/filesystem/10.6.28.135"
# ssh login to remote server
backup_server="root@10.6.28.28"
# set ssh options for backup server
ssh_option="-i /etc/ssh/ssh_host_rsa_key -p 22 -oStrictHostKeyChecking=no"
# log dir on local machine
#log_directory="/tmp/backup-filesystem-10.6.28.135"
log_directory="/tmp/"
# exclude file on local machine
exclude=""


# ------ END OF CONFIGURATION VARIABLES ------

# the following two variables should not need modification
datetime=`date +%Y%m%d%H%M%S`
date=`date +%Y%m%d`

# set log_directory for local backup logs
test -d ${log_directory} || mkdir -p ${log_directory}

# check directories exist and are accessible
ssh ${ssh_option} ${backup_server} "test -e $remote_destination || mkdir -p $remote_destination"

# make directory for this snapshot
ssh ${ssh_option} ${backup_server} "mkdir $remote_destination/$datetime-incomplete" || { echo "Could not create snapshot directory"; exit 1; }

# Refer:
#rsync -u -r -v -e ssh --progress --delete --chmod=D775 /path/to/documents/* your_server_name@your_domain:~/public_html/documents/ --exclude=.htaccess --exclude=.htaccess~
#rsync -azurR -e "ssh -i /etc/ssh/ssh_host_rsa_key -p 22 -oStrictHostKeyChecking=no" --log-file=/tmp/rsync.log --delete --delete-excluded testdir 10.6.28.28:/data/backup/filesystem/10.6.28.135


# do the rsync
# -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
# -r, --recursive             recurse into directories
# -R, --relative              use relative path names
# -u, --update                skip files that are newer on the receiver
# -z, --compress              compress file data during the transfer
rsync -azurR \
    -e "ssh $ssh_option" \
    --log-file=${log_directory}/backup_filesystem_rsync_${datetime}.log \
    --delete --delete-excluded \
    ${datadir_to_backup} \
    ${backup_server}:${remote_destination}/${datetime}-incomplete/

# change name of directory once rsync is complete
ssh ${ssh_option} ${backup_server} "mv $remote_destination/$datetime-incomplete $remote_destination/$datetime" || { echo "Could not rename directory after rsync"; exit 1; }

# link current to this backup
ssh ${ssh_option} ${backup_server} "test ! -d $remote_destination/current || rm -f $remote_destination/current" || { echo "Could not remove current backup link"; exit 1; }
ssh ${ssh_option} ${backup_server} "ln -s $remote_destination/$datetime $remote_destination/current" || { echo "Could not create current backup link"; exit 1; }

# remove backups older than 10 days
ssh ${ssh_option} ${backup_server} "find $remote_destination/* -maxdepth 0 -type d -mtime +10 -exec rm -rf {} \;" || { echo "Could not remove old backups"; exit 1; }

# remove local log files older than 10 days
find ${log_directory}/* -maxdepth 0 -type f -name *.log -mtime +10 -exec rm -rf '{}' \; || { echo "Could not remove old log files"; exit 1; }

declare -x PATH=${old_PATH}



tag:Linux备份,远程备份,rsync备份
--end--



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-245784-1-1.html 上篇帖子: 【AD】AD域Powershell批量添加用户组组织单元20160208 下篇帖子: shell脚本实现LLD监控 current 服务器 Linux 命名
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表