Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 2084  / 2 Years ago, sun, october 30, 2022, 12:37:41

I would like to create backup from directory using cron. I wrote following command:




cd path && sudo zip -r "backup-$(date +"%Y-%m-%d").zip" git/ && sudo mv "backup-$(date +"%Y-%m-%d").zip" /backup/


when I call it from commandline then everything is ok. But when I put it to cron then it does not work. Log contains only part of this command and nothing more. What shall I do?



Crontab content:



* * * * * cd path && zip -r "backup-$(date +"%Y-%m-%d").zip" git/ && mv "backup-$(date +"%Y-%m-%d").zip" /backup/


More From » bash

 Answers
1

As requested i move this to an answer:



Put the commands you wan to run in a .sh file, for example:



1. vi test.sh

cd path && sudo zip -r "backup-$(date +"%Y-%m-%d").zip" git/ && sudo mv "backup-$(date +"%Y-%m-%d").zip" /backup/


Save and exit.



Run on a console



2. chmod +x test.sh


Call file fron cron with the periocity you want:



3. crontab -e

#Run every hour
0 * * * * /path/to/file/test.sh


Make sure the the user that uses cron to execute the file, has permissions to run the file.


[#32494] Sunday, October 30, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eballxeye

Total Points: 370
Total Questions: 91
Total Answers: 139

Location: Suriname
Member since Sat, Jan 1, 2022
2 Years ago
eballxeye questions
Sun, Jan 8, 23, 18:23, 1 Year ago
Sun, Sep 11, 22, 23:24, 2 Years ago
Tue, Dec 14, 21, 07:15, 2 Years ago
;