0

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
jim
  • 1
  • 1
  • Just remove the `sudo` from the script and instead use `sudo` when launching: `sudo mybackup.sh`. See [How do I run a 'sudo' command inside a script?](https://askubuntu.com/q/425754). Note that you also need to tell `tar` what to archive: `tar -czvf "/home/userName/Documents/Backup_files.tar.gz" *`. Or, better, don't `cd` at all and just run `tar -czvf "/home/userName/Documents/Backup_files.tar.gz" /var/www/myFolder/` – terdon Mar 18 '22 at 12:49
  • @terdon, oh wow it was that simple. Embarassing... Thank you! – jim Mar 18 '22 at 12:52
  • No worries, we all need to start from somewhere :) – terdon Mar 18 '22 at 13:05

0 Answers0