Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nobodypb
Created February 8, 2017 22:59
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nobodypb/fc3e70b535bcd95b5de7659d6fbda434 to your computer and use it in GitHub Desktop.
Save nobodypb/fc3e70b535bcd95b5de7659d6fbda434 to your computer and use it in GitHub Desktop.
Script for moving packages on a synology disk station from one volume to another - EXPERIMENTAL!
#!/bin/bash
SOURCE=/volume1
DEST=/volume2
APPDIR=\@appstore
ASK=true
while getopts ":y" opt; do
case $opt in
y)
ASK=false
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
echo "This will move packages from $SOURCE to $DEST"
echo ""
if [ ! -d $DEST/$APPDIR ]; then
mkdir $DEST/$APPDIR
chmod 777 $DEST/$APPDIR
echo "New app directory created"
fi
echo "Searching for packages on $SOURCE..."
for d in /var/packages/*
do
[ -d "$d" ] || continue
app=$(basename $d)
symlink=$d/target
if [ ! -e "$symlink" ]; then
echo "$app has no symlink"
continue
fi
if [ ! -e "$SOURCE/$APPDIR/$app" ]; then
echo "$app is not in source directory"
continue
fi
if [ ! "$(readlink $symlink)" -ef "$SOURCE/$APPDIR/$app" ]; then
echo "$app: Symlink doesn't point to source directory"
continue
fi
echo "Found $app"
file_warnings=$(find $d -type f -exec grep -l "$SOURCE" {} \;)
if [ ! -z $file_warnings ]; then
echo "Be careful!"
echo "The following files contain '$SOURCE':"
echo "$file_warnings"
fi
if $ASK; then
read -s -p "`echo $'\t'`Move $app? (y/n)`echo $'\n \b'`" -n 1 -r
[[ $REPLY =~ ^[Yy]$ ]] || continue
echo ""
fi
echo "`echo $'\t'`Moving $app..."
synopkgctl stop $app
cp -rp $SOURCE/$APPDIR/$app $DEST/$APPDIR/
rm -f $symlink
ln -s $DEST/$APPDIR/$app $symlink
mv $SOURCE/$APPDIR/$app $SOURCE/$APPDIR/__$app
synopkgctl start $app
echo "`echo $'\t'`Done. Backup in: $SOURCE/$APPDIR/__$app"
echo ""
done
echo "Done for all."
echo ""
echo "If you want to remove $SOURCE you should also move the following:"
echo ""
for f in /var/services/*
do
[ -L $f ] || continue
target=$(readlink $f)
if echo "$target" | grep -q $SOURCE; then
echo "System service symlink $f to $target"
fi
done
@blitzdesigner
Copy link

blitzdesigner commented Mar 28, 2017

Hi @nobobypb,
Thank you for your script, worked good so far.
But I am not sure what to do with the message at the end:
###########
If you want to remove /volume1 you should also move the following:

System service symlink /var/services/download to /volume1/@Download
System service symlink /var/services/pgsql to /volume1/@database/pgsql
System service symlink /var/services/printer to /volume1/@spool
System service symlink /var/services/tmp to /volume1/@tmp
########

I was moving from /volume1 to /volume2, so shouldnt be there /Volume2/ in the end?
and, how do I move those symlinks?

thank you for a little hint :-)

@rdhoore108
Copy link

I had to replace if [ ! -e "$symlink" ]; then by if [ ! -h "$symlink" ]; then otherwise I got the "has no symlink" error for all packages, but once I had that figured out, it worked great, thanks!

@burgerga
Copy link

burgerga commented Oct 6, 2018

Thanks for the handy script! It only had trouble moving "Plex Media Server" because of the spaces, but did that manually (spaces cause issues at multiple places in the script)

@mera461
Copy link

mera461 commented Dec 1, 2018

Another helpful command for changing all symlinks from /volume1 to /volume2 is:

find . -lname '/volume1/*' -printf '%l\0%p\0' | sed -z 's|^/volume1|/volume2|;n' | xargs -r0n2 ln -sfT

@shaarkys
Copy link

@nobodypb thank you, worked fine !

@superdale007
Copy link

Sooooo just to be clear here...If I want to move everything off of Vol1 and Vol2 to be on Vol3, I'd run this script twice (changing the source each time to vol1 then vol2)? After it's done, can I then delete vol1 and vol2 and add their space to vol3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment