I always wanted to use stored passwords from my keyring for accessing SMB shares in scripts (backups) on my laptop. My aim was not to expose the passwords in files and use anacron to run the backups. After some testing I came up with this:
- Mount your share once via Nautilus and store the password in the keyring
- try
gio mount smb://<server_name>/<share_name>
, what should work without password in your GNOME session
- Use the following code in backup scripts:
Example tested on Ubuntu 19.04:
# set the dbus address
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
# export dbus address to get access to user space keyring
export DBUS_SESSION_BUS_ADDRESS
# use new gnome user space mount tool (gvfs-mount is deprecated)
gio mount smb://<server_name>/<share_name>
#sync from gvfs created mount point to home dir
rsync -rav /var/run/user/$(id -u)/gvfs/smb-share\:server\=<server_name>\,share\=<share_name>/<folder>/ ~/<sync_dest>/
When running the script via anacron from /etc/cron.daily you need to use the user that has access to the keyring, e.g.:
su -c /home/user/scripts/rsync_sript.sh user