Created
November 8, 2018 05:15
-
-
Save cgpu/bdb039db053b6e8b19ee16f18db3352e to your computer and use it in GitHub Desktop.
Remote automatic backup no authe required RSA key Apache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # author: cgpu | |
| # *NOTE*: | |
| # PLEASE FIND ATTACHED AT THE tail OF THE FILE AN APPENDIX OF | |
| # TASKS/COMMANDS THAT WERE PERFORMED OUTSIDE OF THE .sh FILE FOR THE CRONJOB. | |
| # STEP 0: | |
| #___________________________________________________________________________________________________________________ | |
| #ASSIGNING DATE COMMAND IN THE REQUESTED FORMAT YYYYmmdd IN A VARIABLE | |
| #COMMAND + SYNTAX iNFO FOUND HERE: http://bit.ly/date_in_dir_name | |
| TODAY=`date +%Y\%m\%d` | |
| #DESDIR | |
| #SRCDIR | |
| # STEP 1: | |
| #___________________________________________________________________________________________________________________ | |
| #CREATE .tgz FILE WITH THE CONTENTS OF THE LOCAL DIR NAMED "MyFiles" | |
| tar -zcvf /home/user14/MyFiles-"$TODAY".tgz /home/user14/MyFiles | |
| # STEP 2: | |
| #___________________________________________________________________________________________________________________ | |
| #STORE SIZE OF THE .tgz FILE IN A VARIABLE, IN ORDER TO CHECK LATER IF IT "FITS" IN THE REMOTE MACHINE'S DISK SPACE | |
| TGZSIZE=$(ls -l /home/user14/MyFiles-"$TODAY".tgz | awk '{print $5}') | |
| # STEP 3: | |
| #___________________________________________________________________________________________________________________ | |
| #Checksum for the created .tgz stored in a variable called LOCALSUM | |
| #WE WILL BE LATER ON COMPARING THIS CHECKSUM WITH THE FILE'S IN THE DES DIR | |
| #COMMAND + (parentheses) SYNTAX iNFO FOUND HERE: http://bit.ly/cmmnd_in_var | |
| #*NOTE*: using redirection <, because if not, the full path (instead of a dash -) | |
| #will appear on stdout and will be stored as part of the checksum var; | |
| #since full path obviously doesn't match for our file on remote Vs local, | |
| #storing full path next to the checksum, would always result in mismatching LOCALSUM Vs REMOTESUM | |
| LOCALSUM=$(sha256sum < /home/user14/MyFiles-"$TODAY".tgz) | |
| # STEP 4: | |
| #___________________________________________________________________________________________________________________ | |
| #BEFORE .tgz FILE TRANSFER, CHECKING DISK SPACE AVAILABILITY IN THE REMOTE MACHINE: | |
| #df command, tailored into the following 1-liner will give us the kb of available disk space for the remote machine | |
| # COMMAND + SYNTAX iNFO FOUND HERE: http://bit.ly/df_minus_k | |
| # [i]: df -k | |
| # [o]: $1:Filesystem $2:1K-blocks $3:Used $4:Available 5:Use% $6:Mounted | |
| # We will need $4 | |
| REMOTEDISK=$(ssh cgpu@111.111.111.111 -p30022 df -k /home/ | tail -1 | awk '{print $4}') | |
| # STEP 5: | |
| #___________________________________________________________________________________________________________________ | |
| #NOW ALL WE HAVE TO DO IS ARITHMETICALLY COMPARE THE REMOTEDISK Vs TGZSIZE kb number TO DECIDE | |
| #IF THERE'S ADEQUATE SPACE FOR OUR .tgz FILE IN THE REMOTE MACHINE | |
| #THE RATIONALE IS: IF WE KNOW THAT the REMOTE DISK SPACE IS INADEQUATE, WHY ATTEMPT SENDING THE FILE ANYWAY? | |
| #INSTEAD, WE WILL BE SENDING AN EMAIL TO INFORM ABOUT THE LACK OF SPACE. | |
| #THE LOOP GOES LIKE THIS: | |
| #IF : CHECK FOR ZERO SPACE IN REMOTE, aka "$REMOTEDISK" -eq 0 | |
| #ELIF: CHECK FOR INADEQUATE SPACE, aka "$REMOTEDISK" -lt "$TGZSIZE" | |
| #ELSE: PROCEED TO TRASNFER | |
| #NEST ANOTHER IF TO CHECK FILE INTEGRITY IN THE DES DIR | |
| # COMMAND + SYNTAX iNFO - Arithm.Comparison: http://bit.ly/arithm_cmparson (-eq -gt -lt, from here as well) | |
| # COMMAND + SYNTAX iNFO - SCP: http://bit.ly/scp_examples | |
| # COMMAND + SYNTAX iNFO - mail: http://bit.ly/mail_minu | |
| # COMMAND about THE bit-bucketed STDOUT : http://bit.ly/spare_me_the_stdout | |
| #IF DISK SPACE IS OK, THEN PROCEED TO TRASFER AND EVALUATE SRC AND DES FILE CHECKSUMS. | |
| #HENCE THE NESTED LOOP IN THE END, THAT SHOULD ONLY BE EVALUATED WHEN SCP is True. | |
| if [ "$REMOTEDISK" -eq 0 ] #checked that it works and the email is being sent, by changing to -ne, both for if and elif | |
| then | |
| mail -s "Remote Disk Full;cannot proceed with Backup" user1 < /dev/null | |
| elif [ "$REMOTEDISK" -lt "$TGZSIZE" ] | |
| then | |
| mail -s "Remote Disk Space inadequate;cannot proceed with Backup" user1 < /dev/null | |
| else | |
| scp /home/user14/MyFiles-"$TODAY".tgz user14@139.91.75.10:/home/user14/Backup/ && REMOTESUM=$(ssh user14@139.91.75.10 -p30022 sha256sum < /home/user14/Backup/MyFiles-"`date +%Y\%m\%d`".tgz) #sorry about this one line :/ | |
| # STEP 6: | |
| #___________________________________________________________________________________________________________________ | |
| #Given else, from STEP 5 | |
| #WE NEED TO CHECK THAT THE .tgz FILE HAS BEEN TRANSFERED UNALTERED. | |
| #WE WILL CONNECT via ssh TO REMOTE MACHINE | |
| #in order to get checksum of the .tgz in the destination dir, aka Backup dir. | |
| #ENTER NESTED LOOP: | |
| if [ "$LOCALSUM" == "$REMOTESUM" ] #cannot use -eq here because checksums are not integers | |
| then | |
| mail -s "Backup successfull :)" user1 < /dev/null #this just denotes that yes, we know it's empty and we dont mind. | |
| else | |
| mail -s "Backup failed :/" user1 < /dev/null | |
| fi | |
| fi | |
| # STEP 7: | |
| # ___________________________________________________________________________________________________________________ | |
| # After the attempt for scp, check for scp exit code errors: | |
| # A list with exit status codes for errors can be found here: /home/user14/scp_exit_status.txt | |
| # which was initially found here: http://bit.ly/scp_exit_status_list | |
| SCPEXIT=$? | |
| if [ $SCPEXIT -eq 4 ] #if connection to host failed | |
| then | |
| mail -s "Backup failed; could not establish connection to remote machine." | |
| elif [ $SCPEXIT -eq 74 ] #if connection failed | |
| then | |
| mail -s "Backup failed; could not establish connection to remote machine." | |
| elif [ $SCPEXIT -eq 5 ] #if connection broken | |
| then | |
| mail -s "Backup failed; connection to remote machine was interrupted." | |
| fi | |
| #*NOTE*: Sample failed email messages can be found unread at user14 inbox | |
| # ALSO, TASKS IMPLEMENTED OUTSIDE THE .sh FILE THAT WILL BE cronjob-ed: | |
| # COMMANDS ARE DENOTED WITH 2 HASHTAGS: | |
| # CREATE DIR AND FILES | |
| ## mkdir MyFiles Backup | |
| ## cd MyFiles | touch file_0_scWv.txt file_1_ntHv.txt file_2_blMd.txt #then used emacs to add sth random | |
| # GENERATE PUBLIC-PRIVATE KEY PAIR FOR LOGIN-LESS SCP #assymetric ftw :) | |
| # COMMAND + SYNTAX iNFO - PUBLIC/PRIVATE KEY AUTH: http://bit.ly/public_private_pair | |
| ## ssh-keygen -t rsa | |
| # left evrything blank, no passphrase, no nothing | |
| # identification in: /home/user14/.ssh/id_rsa. | |
| # public key: in /home/user14/.ssh/id_rsa.pub. | |
| # THEN: | |
| ##cat .ssh/id_rsa.pub | ssh user14@Bioinfo-Grad-Server 'cat >> .ssh/authorized_keys' | |
| # entered passwd at this point | |
| # SUBSEQUENTLY PROCEEDED TO SCP TRANSFER, | |
| # TO ALLOW PERMISSIONS FOR FUTURE TRANSFERS | |
| # Got the following message: | |
| # The authenticity of host '111.111.111.111 (111.111.111.111)' can't be established. | |
| # ECDSA key fingerprint is 00:df:00:fc:00:e4:00:00:f6:00:3e:c6:8c:ab:00:00. | |
| # strange bc my cmmand had rsa | |
| # Are you sure you want to continue connecting (yes/no)? yes | |
| # Warning: Permanently added '111.111.111.111' (ECDSA) to the list of known hosts. | |
| # MyFiles-20170215.tgz 100% 1827KB 1.8MB/s 00:00 | |
| # Authentication established, ready for automated backups. | |
| # ONCE .sh FILE WAS READY, | |
| # ALSO MADE IT EXECUTABLE: | |
| ## chmod u+x user14finalex.sh | |
| # and made sure the file is somewhere in home dir | |
| # FINAL STEP: SETTING UP AS CRONJOB | |
| # used readlink cmmand to get full path of the .sh file: | |
| ## [i]: readlink -f user14finalex.sh | |
| # [o]: /home/user14/user14finalex.sh | |
| ## crontab -e | |
| ## 55 23 * * * /bin/bash /home/user14/user14finalex.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment