patternbashMinor
Push backup script
Viewed 0 times
scriptpushbackup
Problem
I just wrote a little bash script for rsync push backups of my laptop to my Synology-Diskstation at home.
Since I am a bash beginner and don't have any experience with
The hardrive of my laptop is encrypted and the backup should be encrypted as well.
Here is the script:
``
echo "${bold}Mounting encrypted backup volume${normal}"
ssh -n -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "/usr/syno/sbin/synoshare --enc_mount MyuserBackup $pass"
sleep 5
echo "${bold}Checking and eventually creating dirs on backup server${normal}"
## Creating directories
while read dir; do
echo "Create incomplete$dir if not existent"
## http://stackoverflow.com/questions/9393038/ssh-breaks-out-of-while-loop-in-bash
ssh -n -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "mkdir -p /volume1/MyuserBackup/rsync_Backup/incomplete$dir"
done < $dirlist
while read dir; do
echo "${bold}Backing up $dir${normal}"
time sudo rsync -avX -u --compress-level=0 --stats -h --exclude-from=/home/myuser/bin/excludelist --numeric-ids "$@" --link-dest=/volume1/MyuserBackup/rsync_Backup/current$dir --rsh="ssh -i /home/myuser/.ssh/id_rsa" $dir root@192.168.0.70:/volume1/MyuserBackup/rsync_Backup/incomplete$dir
done < $dirlist
echo $date
echo "${bold}Updating Date-Structure${normal}"
echo "mv /volume1/MyuserBackup/rsync_Backup/incomplete /volume1/MyuserBackup/rsync_Backup/back-$date"
ssh -t -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "mv /volume1/MyuserBackup/rsync_Backup/incomplete /volume1/MyuserBackup/rsync_Backup/back-$date"
echo "rm -f /volume1/MyuserBackup/rsync_Backup/current"
ssh -t -i /home/myuser/.ssh/id_rsa root@19
Since I am a bash beginner and don't have any experience with
ssh, rsync and backups I am asking for suggestions how to improve the performance, reliablility, usability and security of my script and for suggestions to improve the coding style. I have listed some problems and notes below. The hardrive of my laptop is encrypted and the backup should be encrypted as well.
Here is the script:
``
#!/bin/bash
bold=tput bold
normal=tput sgr0
dirlist=/home/myuser/bin/dirlist
pass=mypassword
date=date "+%Y-%m-%dT%H_%M_%S"`echo "${bold}Mounting encrypted backup volume${normal}"
ssh -n -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "/usr/syno/sbin/synoshare --enc_mount MyuserBackup $pass"
sleep 5
echo "${bold}Checking and eventually creating dirs on backup server${normal}"
## Creating directories
while read dir; do
echo "Create incomplete$dir if not existent"
## http://stackoverflow.com/questions/9393038/ssh-breaks-out-of-while-loop-in-bash
ssh -n -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "mkdir -p /volume1/MyuserBackup/rsync_Backup/incomplete$dir"
done < $dirlist
while read dir; do
echo "${bold}Backing up $dir${normal}"
time sudo rsync -avX -u --compress-level=0 --stats -h --exclude-from=/home/myuser/bin/excludelist --numeric-ids "$@" --link-dest=/volume1/MyuserBackup/rsync_Backup/current$dir --rsh="ssh -i /home/myuser/.ssh/id_rsa" $dir root@192.168.0.70:/volume1/MyuserBackup/rsync_Backup/incomplete$dir
done < $dirlist
echo $date
echo "${bold}Updating Date-Structure${normal}"
echo "mv /volume1/MyuserBackup/rsync_Backup/incomplete /volume1/MyuserBackup/rsync_Backup/back-$date"
ssh -t -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "mv /volume1/MyuserBackup/rsync_Backup/incomplete /volume1/MyuserBackup/rsync_Backup/back-$date"
echo "rm -f /volume1/MyuserBackup/rsync_Backup/current"
ssh -t -i /home/myuser/.ssh/id_rsa root@19
Solution
DRY - Don't Repeat Yourself.
You repeat certain things a lot. I recommend extracting a function for your ssh commands:
This function should have some appropriate error handling too.
Then, your code can become simplified like:
Error Handling
You should include error handling always in bash scripts.
RSync issues, etc.
rsync builds up a list of files to transfer, calculates the smallets dataset that needs to be migrated to accomplish the transfer, then it does the copy. If files change between when the calculation is done, and the transfer is done, it gives different errors/warnings.
This is what is happening with you, I believe.
Performance
Depending on the rsync options, it can take a while to both build the copy-list, and do the copy. Even though it may be inconvenient, you probably cannot beat rsync for performance.
Check each of your options to ensure they are the best. Kill those options that you don't need.
You repeat certain things a lot. I recommend extracting a function for your ssh commands:
function remotessh {
echo "Running ssh : " + "$@"
ssh -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "$@"
}This function should have some appropriate error handling too.
Then, your code can become simplified like:
echo "${bold}Mounting encrypted volume${normal}"
remotessh -n "/usr/syno/sbin/synoshare --enc_mount MyuserBackup $pass"
.....
remotessh -t "mv /volume1/MyuserBackup/rsync_Backup/incomplete /volume1/MyuserBackup/rsync_Backup/back-$date"
remotessh -t "rm -f /volume1/MyuserBackup/rsync_Backup/current"
remotessh -t "ln -s /volume1/MyuserBackup/rsync_Backup/back-$date /volume1/MyuserBackup/rsync_Backup/current"
echo "Showing the current dir structure:"
remptessh -t "ls -lhrt /volume1/MyuserBackup/rsync_Backup/"
echo "${bold}Unmounting encrypted volume${normal}"
remotessh -t "/usr/syno/sbin/synoshare --enc_unmount MyuserBackup"Error Handling
You should include error handling always in bash scripts.
RSync issues, etc.
rsync builds up a list of files to transfer, calculates the smallets dataset that needs to be migrated to accomplish the transfer, then it does the copy. If files change between when the calculation is done, and the transfer is done, it gives different errors/warnings.
This is what is happening with you, I believe.
Performance
Depending on the rsync options, it can take a while to both build the copy-list, and do the copy. Even though it may be inconvenient, you probably cannot beat rsync for performance.
Check each of your options to ensure they are the best. Kill those options that you don't need.
Code Snippets
function remotessh {
echo "Running ssh : " + "$@"
ssh -i /home/myuser/.ssh/id_rsa root@192.168.0.70 "$@"
}echo "${bold}Mounting encrypted volume${normal}"
remotessh -n "/usr/syno/sbin/synoshare --enc_mount MyuserBackup $pass"
.....
remotessh -t "mv /volume1/MyuserBackup/rsync_Backup/incomplete /volume1/MyuserBackup/rsync_Backup/back-$date"
remotessh -t "rm -f /volume1/MyuserBackup/rsync_Backup/current"
remotessh -t "ln -s /volume1/MyuserBackup/rsync_Backup/back-$date /volume1/MyuserBackup/rsync_Backup/current"
echo "Showing the current dir structure:"
remptessh -t "ls -lhrt /volume1/MyuserBackup/rsync_Backup/"
echo "${bold}Unmounting encrypted volume${normal}"
remotessh -t "/usr/syno/sbin/synoshare --enc_unmount MyuserBackup"Context
StackExchange Code Review Q#29429, answer score: 7
Revisions (0)
No revisions yet.