A collection of shell backup scripts under Linux

Source: Internet
Author: User
Tags create directory

A collection of shell backup scripts under Linux

When it comes to the shell we are not unfamiliar, the shell is Linux under the batch script, like the VBS script under Windows and so on, can help us to complete some automated tasks, we also introduced some shell scripts to help us to complete some of the planning tasks, of course, the planning task is not right, is to perform some automated tasks by scheduling a task to invoke the shell script. Nonsense not much to say, to point to the actual, today, we mainly introduce the environment or work encountered problems, through remote to complete some restore tasks,

1. First of all, we need to overwrite the weaver.proternt file in the local backup directory to the specified directory at a timed restore;

Restore the/oafs/bkfiles/abc.txt file to a local/oafs/weaver/ecology/abc.txt

#!/bin/bashbk_profeam=/oafs/bkfiles/abc.txtlocal_profeam=oafs/weaver/ecologyfunction copy () {' Which CP '-RF $BK _ Profeam $LOCAL _profeam}copy

After writing, we need chmod 770 xxxx

2. We need to unzip and overwrite the backup compressed files in the local/oafs/weaver_backup directory to the/OAFS directory under the remote server 192.168.6.38.

If the target exists, we need to unzip the overwrite operation.

650) this.width=650; "title=" image "style=" Border-top:0px;border-right:0px;background-image:none;border-bottom : 0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px; "border=" 0 "alt=" image "src="/HTTP/ S3.51cto.com/wyfs02/m00/85/6f/wkiol1ejj0db5489aaehh1nefbo380.png "height="/>

#!/bin/bashlocalbakdir=/oafs/weaver_backuprmtrestdir=/oafsrmt_host=192.168.6.38rmt_user=rootrmt_cmd= "$ (which  SSH)  [email protected] $RMT _host "function is_alive ()  {     ' which  ping '  -c 4  $RMT _host >/dev/null 2>&1    if [   "$?"  -ne 0 ]; then        echo 2     else        echo 0         fi}function mktmpdir ()  {     $RMT _cmd mkdir -p / Tmp/oafs}function rmvtmpdir ()  {     $RMT _cmd rm -rf /tmp/oafs/*} Function get_last_targz ()  {    echo $ (ls -lt  $LOCALBAKDIR  |  awk {' print $9 '} |grep -v ^$ |head -n 1)}function copy ()  {    yum install -y openssh-clients >/dev/null 2>&1      $RMT _cmd yum install -y openssh-clients >/dev/null 2>&1     scp -q  $LOCALBAKDIR/$ (get_last_targz)  [email protected] $RMT _host:/ tmp/oafs/     $RMT _cmd tar zxvf /tmp/oafs/$ (get_last_targz)  -C $ rmtrestdir}if [  "$ (is_alive)"  -eq 0 ]; then    mktmpdir     copy    rmvtmpdirelse    echo  "$RMT _host  can not be accessed via port 22, please check "fi

Note: Because it is a remote server, we execute the script usually need to use a password to verify, because it is an automated task, so we can not enter the password, so we need to use Ssh-key to verify;

So we need to execute the registry key file on the server running the script:

SSH-KEYGEN-T RSA return

650) this.width=650; "title=" clip_image001 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001" src= "http://s3.51cto.com/wyfs02/M01/85/70/wKiom1ejJ0CgoB_EAABkO8g4w8s355.png" height= "/>"

Create directory, authentication directory, and then return all the way.

650) this.width=650; "title=" clip_image002 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image002" src= "Http://s3.51cto.com/wyfs02/M01/85/70/wKiom1ejJ0GSLQ4VAAFl8skLYk4949.png" height= "345"/>

A pair of key files are generated in the/ROOT/.SSH directory

Id_rsa private Key

Id_rsa.pub Public Key

650) this.width=650; "title=" clip_image003 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image003" src= "http://s3.51cto.com/wyfs02/M01/85/6F/wKioL1ejJ0LgMizQAAA4x5a709o477.png" height= "/>"

Ssh-copy-id IPAddress

Only on the machine where the script executes; the public key is automatically copied to the target server;

650) this.width=650; "title=" clip_image004 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image004" src= "Http://s3.51cto.com/wyfs02/M02/85/70/wKiom1ejJ0KDPXD5AACx0RliY74450.png" height= "151"/>

This allows you to copy data remotely by running the script;

3. We remotely copy and overwrite all contents of local/oafs/files/pdf file to target folder/oafs/pdf;

#!/bin/bashrmt_host=192.168.6.38rmt_user=rootrmt_cmd= "$ (which ssh) [email protected] $RMT _host" local_pdf=/oafs/ Files/pdfrmt_pdf=/oafs/pdffunction is_alive () {' Which ping '-C 4 $RMT _host >/dev/null 2>&1if ["$?"-ne 0]; the Necho 2elseecho 0fi}function Copy () {scp-q-R $LOCAL _pdf/* [email protected] $RMT _host: $RMT _pdf}if ["$ (is_alive)"-eq 0] ; Thencopyelseecho "$RMT _host can not is accessed via port, please check" fi

Finally, we can define the scheduled tasks through the crontab-e to execute;

Note: If the current server does not have the crontal command, we need to install;

Yum Install Crontals
Crontab–e format We have introduced the last time, so we will not introduce more, we simply say a few columns can be:

Execute the script 23:10 every night;

5 per week of 23:30 execution scripts and 6 1:10 execution scripts per week

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/85/70/wKiom1ejJ0PjW6TdAADFqhbkMqU132.png "height=" 155 "/>

This article from "Gao Wenrong" blog, declined reprint!

A collection of shell backup scripts under Linux

Related Article

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.