web123456

Lab Building Linux Basics for Beginners (New Edition) - Backup Logging

title

Ming is a server administrator, he needs to backup the forum data (here we use logs instead) every day, backup the logs of the day and delete the previous logs. And after backup the file name is in year-month-day format. Under /var/log/.

goal

  1. Adding scheduled tasks for shiyanlou users
  2. Regular backup to /home/shiyanlou/tmp/ directory at 3:00am every day
  3. The naming format is year-month-day, for example, if today is April 1, 2017, then the file name is 2017-04-01

draw attention to sth.

  1. date
  2. crontab
  3. cp (backup)
  4. Just write a command in the crontab, not a script.
  5. Note that crontab's scheduled user
    ->crontab -e means to add a task plan for the current user.
    ->sudo crontab -e means to add a task plan for the root user

prescription

1. Add tmp directory
$ mkdir tmp
  • 1
2. Start rsyslog
$ sudo service rsyslog start
  • 1
3. Start crontab
$ sudo cron -f &
  • 1
4. Edit the crontab file
$ crontab -e
  • 1

/usr/bin/select-editor: 32: /usr/bin/select-editor: gettext: not found
1-3 []: 1 (or 3)
Press a to start editing
Enter it on the last line of the file:

0 3 * * * cp /var/log/alternatives.log /home/shiyanlou/tmp/$(date+ \%Y\%m\%d)  
  • 1

Ctrl+C to exit edit mode;
Shift+':', type: wq , exit.