Thursday, April 25, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 2831  / 2 Years ago, mon, april 4, 2022, 2:42:07

Me again with another command-line script question :)



I made a helper script that looks like this



echo 'target.bak -> target.old_bak'
mv target.bak target.old_bak
echo 'target -> target.bak'
mv target target.bak
echo 'git clone:target -> target'
git clone ssh://Shark@mcs15:29418/our-trunk-target target
cd target
echo 'Managing hooks...'
scp -p -P 29418 Shark@mcs15:/hooks/commit-msg .git/hooks/
echo '... done. Enjoy the new clone while it lasts...'


This is pretty basic but it gets my point across - it keeps the last two clones and makes a new one, I need to retain my latest clone in the target folder because i symlinked my source folder in my eclipse project to point there :D



I would like to make an improvement to this script - instead of renaming target.bak -> target.old_bak and target -> target.bak I would like the following to happen:



if bakN exists then rename bakN -> bak(N+1)
rename recursivelly bak(N-1) -> bakN
rename target to target.bak
clone new repo into target


So in case I had gone through 6 clones and I'm cloning for the seventh time I'd like this to happen:



target.bak4 -> target.bak5
target.bak3 -> target.bak4
target.bak2 -> target.bak3
target.bak1 -> target.bak2
target.bak0 -> target.bak1
target -> target.bak0
Cloning into 'target'...


This community helped me immensely with my previous mass-diffing question so I hope you will be helpful enough to provide me with a decent way to do this one as well :)



EDIT:



The finalized script looks like this, thanks Wolfie for your major contributions.



#!/bin/bash
MYUSER="Shark"
TRUNK_FOLDER="target-on-trunk"
TARGET_FOLDER="target"

if [ -n "`ls -1d -v ${TARGET_FOLDER}.bak?`" ]; then
echo "Pushing back numbered backups..."
MAXBCK=`ls -1d ${TARGET_FOLDER}.bak* 2>/dev/null | egrep -o "[0-9]+$" | sort -n | tail -n1`
if [ "$MAXBCK" != "" ]; then
for (( c=$MAXBCK; c>=0; c-- ))
do
mv -v ${TARGET_FOLDER}.bak$c ${TARGET_FOLDER}.bak$(($c+1))
done
fi
fi

if [ -e "$TARGET_FOLDER.bak" ]; then
mv -v ${TARGET_FOLDER}.bak ${TARGET_FOLDER}.bak0
fi

if [ -e "$TARGET_FOLDER" ]; then
mv -v ${TARGET_FOLDER} ${TARGET_FOLDER}.bak
fi

echo "git clone: ${TRUNK_FOLDER} -> ${TARGET_FOLDER}"
git clone ssh://${MYUSER}@mcs15:29418/${TRUNK_FOLDER} ${TARGET_FOLDER}
cd ${TARGET_FOLDER}
echo 'Managing hooks...'
scp -p -P 29418 ${MYUSER}@mcs15:/hooks/commit-msg .git/hooks/
echo '... done. Enjoy the new clone while it lasts...'

More From » command-line

 Answers
5

This could help you:



For this files:



target.bak4 -> target.bak5
target.bak3 -> target.bak4
target.bak2 -> target.bak3
target.bak1 -> target.bak2
target.bak0 -> target.bak1


You can do this:



ls -1 target.bak* | awk '{print "mv "$0" "substr($0,0,length)substr($0,length,1)+1}' | sh



For target -> target.bak0 just do cp target target.bak



Update:



I tested this command and works only from 0 -> 19 backup, than the renaming fails... but for what you need is OK :)



Update #2:



Script version:



if [ $# != 1 ]; then
echo 'Usage: $0 target'
exit
fi
if [ `ls -1 $1* 2>/dev/null | grep -c $1` -lt 1 ]; then
echo "No file found"
exit
fi

MAXBCK=`ls -1 $1.bak* 2>/dev/null | egrep -o "[0-9]+$" | sort -n | tail -n1`

if [ "$MAXBCK" != "" ]; then
for (( c=$MAXBCK; c>=0; c-- ))
do
# echo $c $(($c+1))
mv $1.bak$c $1.bak$(($c+1))
done
fi

cp $1 $1.bak0


Example:



wolfy@wolfy-ubuntu:~/tmp$ ll
total 4
-rwxr-xr-x 1 wolfy wolfy 402 May 27 14:43 fresh_clone.sh*
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:10 test
wolfy@wolfy-ubuntu:~/tmp$ ./fresh_clone.sh test
wolfy@wolfy-ubuntu:~/tmp$ ll
total 4
-rwxr-xr-x 1 wolfy wolfy 402 May 27 14:43 fresh_clone.sh*
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:10 test
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:44 test.bak0
wolfy@wolfy-ubuntu:~/tmp$ ./fresh_clone.sh test
wolfy@wolfy-ubuntu:~/tmp$ ll
total 4
-rwxr-xr-x 1 wolfy wolfy 402 May 27 14:43 fresh_clone.sh*
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:10 test
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:44 test.bak0
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:44 test.bak1
wolfy@wolfy-ubuntu:~/tmp$ ./fresh_clone.sh test
wolfy@wolfy-ubuntu:~/tmp$ ll
total 4
-rwxr-xr-x 1 wolfy wolfy 402 May 27 14:43 fresh_clone.sh*
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:10 test
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:44 test.bak0
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:44 test.bak1
-rw-r--r-- 1 wolfy wolfy 0 May 27 14:44 test.bak2


Try with this... but keep in mind that this is only a sample code, for "production" you should add some check before running this :)



This version works with N bak files... not only 19 ;)


[#31026] Tuesday, April 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tisglitter

Total Points: 307
Total Questions: 103
Total Answers: 119

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
tisglitter questions
Sun, Jan 9, 22, 16:18, 2 Years ago
Wed, Jun 1, 22, 18:03, 2 Years ago
Fri, Dec 10, 21, 14:31, 2 Years ago
;