日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

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

發(fā)布時(shí)間:2023/12/19 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LinuxShell脚本之利用rsync+ssh实现Linux文件系统远程备份 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

功能介紹:

該腳本用于定期(結(jié)合crontab一起使用)將本地目錄通過(guò)rsync+ssh傳輸?shù)竭h(yuǎn)程服務(wù)器,每次執(zhí)行都生成一個(gè)帶有以時(shí)間命名的目錄,并且當(dāng)前最新版本的數(shù)據(jù)鏈接到一個(gè)名字叫current的符號(hào)鏈接上,便于查找和恢復(fù)。在數(shù)據(jù)傳輸完成前,會(huì)傳輸?shù)脚R時(shí)目錄下,這個(gè)臨時(shí)目錄被命名為“時(shí)間-incomplete”。超過(guò)10天的備份將被刪除,超過(guò)10天的日志文件也將被刪除。

運(yùn)行原理:

腳本運(yùn)行的核心就在于ssh的互信和rsync命令。利用rsync能實(shí)現(xiàn)壓縮傳輸,節(jié)省傳輸時(shí)間。

感謝:

感謝gregrs-uk提供的初始腳本。

腳本內(nèi)容:

#!/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>' #?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").logUSER="`id?-un`" LOGNAME="$USER" if?[?$UID?-ne?0?];?thenecho?"WARNING:?Running?as?a?non-root?user,?\"$LOGNAME\".?Functionality?may?be?unavailable.?Only?root?can?use?some?commands?or?options" fiold_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備份,遠(yuǎn)程備份,rsync備份

--end--



轉(zhuǎn)載于:https://blog.51cto.com/dgd2010/1826915

總結(jié)

以上是生活随笔為你收集整理的LinuxShell脚本之利用rsync+ssh实现Linux文件系统远程备份的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。