Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Sep 22, 2017

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Dec 15, 2018

@nobodypb thank you, worked fine !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.