Linuxshell script using RSYNC+SSH for remote backup of Linux file system

Source: Internet
Author: User
Tags rsync

Feature Description:

The script is used to transfer the local directory to the remote server on a regular basis (in conjunction with crontab), with each execution generating a time-named directory, and the current version of the data is linked to a symbolic link with the first name, so it is easy to find and recover. Before the data transfer is complete, it is transferred to the TEMP directory, which is named "Time-incomplete". Backups older than 10 days will be deleted and log files older than 10 days will also be deleted.

Operating principle:

The core of the script is the trust of SSH and the rsync command. The use of rsync can achieve compression transmission, saving transmission time.

Thank:

Thanks for the initial script provided by Gregrs-uk.

Script content:

#!/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 < [email protected]> ' # 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&NBSP;-&Nbsp;31) # |  |  | &nbsp------- month  (1 - 12)  OR  jan,feb,mar,apr ...# |  |  |  | &nbsp.---- 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 ]; then    echo " Warning: running as a non-root user, \ "$LOGNAME \".  functionality may  be unavailable. only root can use some commands or options " Fiold_path= $PATHdeclare  -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 spacesdatadir_to_backup= "/ Data/docker "# backup location on remote server# this path should  Not contain spaces, even if they are escapedremote_destination= "/data/ backup/filesystem/10.6.28.135 "# ssh login to remote serverbackup_server=" [email  Protected] "# set&nbSp;ssh options for backup serverssh_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  Machineexclude= "" # ------ END OF CONFIGURATION VARIABLES ------# the  Following two variables should not need modificationdatetime= ' date +%Y%m%d% h%m%s ' date= ' date +%y%m%d ' # set log_directory for local backup logstest  -d ${log_directory} | |  mkdir -p ${log_directory}# check directories exist and are  accessiblessh ${ssh_option} ${backup_server}  "test -e  $remote _destination | |  mkdir -p  $remote _destination "# make directory for this snapshotssh ${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/* [email protected]_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 transferrsync -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 completessh ${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 backupssh ${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 daysssh ${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 daysfind ${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 backup, remote backup, rsync backup

--end--



This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1826915

Linuxshell script using RSYNC+SSH for remote backup of Linux file system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.