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

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

功能介绍:

该脚本用于定期(结合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}

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

--end--

转载地址:http://uincl.baihongyu.com/

你可能感兴趣的文章
Python网络爬虫(七) 深度爬虫CrawlSpider
查看>>
使用selenium抓取华尔街见闻和新浪财经数据
查看>>
ListView与EditText冲突问题解决
查看>>
接口咋整?前端数据药神来也
查看>>
使用Jenkins自动构建Android应用打包并上传
查看>>
Android 混淆简单入门
查看>>
视口相关单位的应用 —— 别说你懂CSS相对单位
查看>>
说说在 Vue.js 中如何绑定样式(class 或 style)
查看>>
如何优雅地在 Java 8 中处理异常
查看>>
Pandas分类数据详解 | 轻松玩转Pandas(5)
查看>>
由自定义事件到vue数据响应
查看>>
前端模块化:CommonJS,AMD,CMD,ES6
查看>>
React Native 性能优化组件-PureComponent
查看>>
CSS3系列-css3之线性渐变初探
查看>>
没错,这个就是那个面试可能会问的"破题"!
查看>>
Hello World
查看>>
浅谈js中的继承和拷贝(下)
查看>>
代码洁癖系列(八):迭代的原则
查看>>
Java集合(七) Queue详解
查看>>
JavaScript 浅谈 button 和 input type=button 的区别
查看>>