0

I want to create an automatic monthly backup on Ubuntu 14.04 LTS - Server Ed., with the following command:

$ tar -cvpz --exclude=/bckupftp --exclude=/ser --exclude=/proc --exclude=/lost+found --exclude=/mnt --exclude=/sys / | split -d -b 750m - /bckupftp/backup20160422-041400.tar.gz

I added a date and time in the backup filename. It would be nice if this could be set to the actual-backup-date/time automatically. For a cronjob set to run on May 01, 2016 at 1am, the backup-filename would be: /bckupftp/backup20160501-010000.tar.gz.

Can somebody help me set up a cronjob for this command ? Thanks, Kevin

Cbhihe
  • 2,761
  • 3
  • 24
  • 47
kevinx
  • 85

2 Answers2

4

To set up the cronjob for yr present non-root user, do in terminal:

$ crontab -e

The above will open yr (non-root) user's crontab with his/her default editor.

Alternatively, to do so for the root crontab, but with yr present (presumably non-root) user environment parameters (default editor, etc.):

$ sudo -i crontab -e

In yr opened crontab, enter a new line:

0 01 01 * * /bin/tar -cvpz --exclude=/bckupftp --exclude=/ser --exclude=/proc --exclude=/lost+found --exclude=/mnt --exclude=/sys / | /usr/bin/split -d -b 750m - /bckupftp/backup$(/bin/date +\%Y\%m\%d-\%H\%M\%S).tar.gz

(I did not check the validity of yr tar cmd.)

Note that:

  • crontab uses a 24-hour clock, military style: 15 means 3pm, 03 means 3am, etc.

  • the above cron entry runs at 01:00 o'clock the first day of every month. In terminal , do: man crontab for more info on crontab formats.

  • in crontab, it's a good habit to prefix cmds with their full path. To find the full path of any non-built-in cmd, do in terminal $ which <cmd>. Thus the tar cmd becomes /bin/tar, split becomes /usr/bin/split, etc..

  • as soon as you save the newly edited crontab, the cron job becomes effective. No need to reboot, log out and back in, or restart whatever service.

  • when saving yr bckup file(s), the time stamp has the following format YYYYMMDD-HHMMSS. If you need it to be more precise, try replacing %S with %s in yr crontab entry.

  • I am certain (meaning "with 100% probability") you can find all the above explained in great details in AU, SE at large as well as in other fora. Remember that StartPage is yr friend.

Cbhihe
  • 2,761
  • 3
  • 24
  • 47
0

Create a file: /etc/cron.monthly/backups.sh make it executable by: $ chmod +x /etc/cron.monthly/backups.sh

to tidy it up, create a variable:

DATE=`date +%Y.%m.%d-%H.%M.%S`                   # eg. 2016.04.22-13.01.59

And then reference your command above with: /bckupftp/backup${DATE}.tar.gz

It would produce a file similar to: /bckupftp/backup2016.04.22-13.01.59.tar.gz

Feel free to remove dots to your liking and needs.

Cbhihe
  • 2,761
  • 3
  • 24
  • 47
  • 1
    Welcome to AU ! Please look up formatting guidelines (https://meta.askubuntu.com/questions/89/style-guide-for-questions-and-answers) on this site, as well as this answer (https://meta.askubuntu.com/questions/13828/formatting-guideline-convention-for-program-names-commands-strings-values/13839#13839) by Eliah Kagan. Doing so will make yr answers and questions even greater. :-) – Cbhihe Apr 23 '16 at 17:23