博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LinuxShell脚本之利用rsync+ssh实现Linux文件系统远程备份
阅读量:6288 次
发布时间:2019-06-22

本文共 4821 字,大约阅读时间需要 16 分钟。

功能介绍:

该脚本用于定期(结合crontab一起使用)将本地目录通过rsync+ssh传输到远程服务器,每次执行都生成一个带有以时间命名的目录,并且当前最新版本的数据链接到一个名字叫current的符号链接上,便于查找和恢复。在数据传输完成前,会传输到临时目录下,这个临时目录被命名为“时间-incomplete”。超过10天的备份将被删除,超过10天的日志文件也将被删除。

运行原理:

脚本运行的核心就在于ssh的互信和rsync命令。利用rsync能实现压缩传输,节省传输时间。

感谢:

感谢提供的。

脚本内容:

#!/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 
' # Blog: http://dgd2010.blog.51cto.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}
 

 

转载于:https://www.cnblogs.com/creater-wei/p/9846145.html

你可能感兴趣的文章
git常用命令
查看>>
3.07-JS合并两个JSON对象
查看>>
VUE2.0 实现移动端在固定区域内的滚动效果
查看>>
angularjs入门(一)
查看>>
环境变量PATH、cp命令、mv命令、cat命令、tac命令、more、less、head、tail
查看>>
bandit系列0--10
查看>>
文本过滤之grep,egreo及fgrep 三剑客及正则表达式
查看>>
实现Singleton模式在C#
查看>>
服务发现:Zookeeper vs etcd vs Consul
查看>>
微软企业项目管理系统技术研讨会
查看>>
Kafka设计篇之消息传输的事务定义
查看>>
我的友情链接
查看>>
使用windows 7 系统安装盘 DOS普通用户提权为管理员
查看>>
老男孩教育每日一题第115天:如何在centos 6下面实现命令补全?效果如下
查看>>
国内可用的yum源
查看>>
linux df -h 命令卡住 解决方法
查看>>
spring是什么,Spring能帮我们做什么
查看>>
Codeforces 861D - Polycarp's phone book
查看>>
FreePortScanner.java
查看>>
HttpURLConnection 文件上传限制
查看>>