I execute a sh script in terminal which backs up some folders. However, these folders are contained within the /var/www/
directory. I use sudo -i
to access this directory but when it's executed in the terminal, the terminal switches to root user and the script pauses. The script will continue when I exit
the root.
Is it possible to use root in the script without it pausing in the terminal?
#!/bin/sh
echo -n "Backing up files..."
sudo -i
cd /var/www/myFolder/
tar -czvf "/home/userName/Documents/Backup_files.tar.gz"
echo -e "\rBacking up files...100%"
exit
sudo
from the script and instead usesudo
when launching:sudo mybackup.sh
. See How do I run a 'sudo' command inside a script?. Note that you also need to telltar
what to archive:tar -czvf "/home/userName/Documents/Backup_files.tar.gz" *
. Or, better, don'tcd
at all and just runtar -czvf "/home/userName/Documents/Backup_files.tar.gz" /var/www/myFolder/
– terdon Mar 18 '22 at 12:49