Para resolver a parte da mensagem de erro da pergunta, você pode optar por executar um script do cron em vez do comando do sistema.
24 9 * * * /usr/local/sbin/sync_data.sh
Crie o arquivo como /usr/local/sbin/sync_data.sh
, fornecendo a propriedade raiz e a permissão de execução: chown root:root /usr/local/sbin/sync_data.sh && chmod 0700 /usr/local/sbin/sync_data.sh
. O conteúdo do script está abaixo.
#!/usr/bin/env bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root." 1>&2
exit 1
fi
# [ ] vs [[ ]] : http://mywiki.wooledge.org/BashFAQ/031
dir_src="/home/fan/Data/"
dir_dst="/media/T/"
# A Boolean to know if running interactively or not
b_interactive=1
if [[ -v PS1 ]] ; then # The "-v PS1" method is for BASH 4.2+
$b_interactive=0 # Could also use [[ -z "$PS1" ]]
fi
# Send messages to console or syslog.
function notify() {
message="$1"
if [[ $b_interactive -eq 1 ]] ; then
echo "$message"
else
# eval combines args into a single string for execution...
eval $cmd_logger -p err "$0: $message"
fi
}
# If the mount point if not currently mounted...
if [[ "$(grep $dir_dst /proc/mounts)" = "" ]] ; then
# Try to mount the directory.
mount $dir_dst
# Send a message to console or syslog.
if [[ $? -ne 0 ]] ; then
notify("$0 failed to mount $dir_dst")
exit 1;
fi
fi
# Create a backup directory if it does not exist
if [[ -d $dir_dst ]] ; then
mkdir -p $dir_dst 2>/dev/null
fi
# A one-way sync to dir_dst, deleting files that no longer exist in DIR_SRC...
rsync -a --delete $dir_src $dir_dst &> /dev/null
# Check the return status of last command...
if [ $? -eq 0 ]; then
notify("$0: the rsync process succeeded.");
else
notify("$0: the rsync process failed.");
fi
unset dir_src
unset dir_dst
unset b_interactive