
Automating backups with a shell script
We will write a shell script and run it periodically from cron to take backups
What if we wrote a shell script that automatically generates a shell script, so it is easy to use whenever a new directory needs rsync?
code.sh
echo "Rsync Source Path :"
read source_path
echo "Rsync Destination Path :"
read des_path
echo '
check=`diff -r source_path des_path -q -x "*.*.*"`
if [ -n "$check" ]
then
addfile_src=`diff -r source_path des_path -q | grep "Files" | cut -d " " -f 2`
addfile_des=`diff -r source_path des_path -q | grep "Files" | cut -d " " -f 4`
addfile_num=`echo $addfile_src | wc -w`
if [ "0" != $addfile_num ]
then
for (( i=1; i<=$addfile_num; i++))
do
target_src=`echo $addfile_src | cut -d " " -f $i`
target_des=`echo $addfile_des | cut -d " " -f $i`
echo -e $i"/"$addfile_num" src:" $target_src"\t\t\tdes:" $target_des
rsync -r --backup --suffix=`date +'.%F_%H-%M-%S'` $target_src $target_des
done
fi
echo "adding file rcync success"
nonefile_dir=`diff -r source_path des_path -q | grep -v "des_path:" | cut -d ":" -f 1 | cut -d " " -f 3`
nonefile_file=`diff -r source_path des_path -q | grep -v "des_path:" | cut -d " " -f 4`
nonefile_num=`echo $nonefile_dir | wc -w`
if [ "0" != $nonefile_num ]
then
for (( i=1; i<=$nonefile_num; i++))
do
target_dir=`echo $nonefile_dir | cut -d " " -f $i`
target_subdir="/"`echo $target_dir | tr -d "source_path"`
target_file="/"`echo $nonefile_file | cut -d " " -f $i`
target_src="source_path"${target_subdir}${target_file}
target_des="des_path"$target_subdir
echo -e $i"/"$nonefile_num "src :" $target_src"\t\t\tdes :" $target_des
rsync -r --backup --suffix=`date +".%F_%H-%S"` $target_src $target_des
done
fi
echo "empty file rcync success"
fi' >> backup.sh
sed -i "s|source_path|$source_path|g" backup.sh
sed -i "s|des_path|$des_path|g" backup.shWhen you run this shell script, a backup.sh file is created, and you can rsync through that shell.
I will not walk through the code separately. If you write an automation script of your own with the shell, it becomes very convenient, so you are welcome to reuse my code, but I hope you will try writing it yourself. And if you can implement it more simply, I hope you will implement it simply and efficiently and use that.
Comments 0
A nickname and a password are required. The same password edits or deletes the comment later. Comments appear after a review.