how can write bash script to get backup

Viewed 29

I want to write a bash script to backup my directories every day

this is my script

$ sudo tar -cvpzf /home/backup.tar.gz /home/data

how can I automate this

1 Answers

As @Fravadona suggested, your best bet would be to use a cron job. For such a short and self-explanatory command, I wouldn't even use a script per se.

You can add a new job just typing in your terminal sudo crontab -e for edit (since you want to run a command with sudo, this opens root's crontab). To better understand how the frequency is configured, you can use for example crontab.guru.

Then, you can add the new entry using your preferred text editor. As suggested in the comments, you could add a timestamp to the tar file name to keep track of it.

For example, a backup that runs every day at 08:00:

0 8 * * * /usr/bin/sudo /usr/bin/tar -cvpzf /home/backup_`date +%s`.tar.gz /home/data

Note: The date +%s command returns a unix timestamp.

Related