smbclient Cannot Remove Non-Empty Remote Directory

Problem: I needed a script to save off backup files, but also remove older backup files. It turns out that smbclient cannot remove remote directories that are not empty and it might not be possible to use wildcard to select remote directories.

Solution: Hardcode older dates

current_date=$(date +"%Y%m%d")
two_months_ago=$(date +"%Y%m%d" --date="2 months ago")
months_end_two_months_ago=$(date +"%Y%m%d" --date="-2 month +1 day")

smbclient //$SHARE_HOST/$SHARE_FOLDER "$SHARE_PASSWORD" -U $SHARE_DOMAIN/$SHARE_USER -c '
lcd remote/folder/;
cd local\folder\;
rm '$two_months_ago'\*;
rmdir '$two_months_ago';
rm '$months_end_two_months_ago'\*;
rmdir '$months_end_two_months_ago';
md '$current_date';
cd '$current_date'\;
mput *;
'

References:

Like what you read? Give Zhao Li a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.