Our features site is undergoing a refresh! Be sure to explore the revamped site and discover our latest product roadmap launching here on Monday, March 18th.

Option to create /daily symlink to newest changing zzzz-yy-xx backup directory when new backup runs

North Captiva shared this idea 7 years ago
Open Discussion

As a server administrator I would like cPanel to provide a symlink (named /daily or /latest, for example) in the /backup directory that would take me to the most recent backup, to allow me to more quickly and consistently identify the most recent backup, rather than manually parsing the folders and files in the directory tree.

This would be especially helpful in situations where I would like to programmatically sync my backups to another location.

Replies (1)

photo
1

I wrote a quick PHP hook that accomplishes this:


Instructions:

Save the following to /root/custom_scripts/backup_hook.php


  1. #!/usr/local/cpanel/3rdparty/bin/php -q
  2. <?php
  3. // file - /root/custom_scripts/backup_hook.php
  4. // Backup Directory with Trailing Slash
  5. define("BACKUP_DIR", "/backup/");
  6. // Any switches passed to this script
  7. $switches = (count($argv) > 1) ? $argv : array();
  8. // Argument evaluation.
  9. if (in_array('--describe', $switches)) {
  10. echo json_encode( describe() );
  11. exit;
  12. } elseif (in_array('--run', $switches)) {
  13. create_symlink();
  14. echo "$status $msg";
  15. exit;
  16. } else {
  17. echo 'backup_hook.php needs a valid switch';
  18. exit(1);
  19. }
  20. // Embed hook attribute information.
  21. function describe() {
  22. $post_backup = array(
  23. 'category' => 'System',
  24. 'event' => 'Backup',
  25. 'stage' => 'post',
  26. 'hook' => '/root/custom_scripts/backup_hook.php --run',
  27. 'exectype' => 'script',
  28. );
  29. return $post_backup;
  30. }
  31. function create_symlink() {
  32. $current_backup = bdir(date("Y-m-d"));
  33. $latest = bdir("latest");
  34. if (is_link($latest)) {
  35. unlink($latest);
  36. }
  37. symlink($current_backup, $latest);
  38. }
  39. function bdir($path) {
  40. return BACKUP_DIR.$path;
  41. }


Set executable:

  1. chmod +x /root/custom_scripts/backup_hook.php


Add the hook:

  1. /usr/local/cpanel/bin/manage_hooks add script /root/custom_scripts/backup_hook.php

If you choose to save the script in a different directory, you will have to edit the file. Also, make sure to edit the BACKUP_DIR up at the top of the script. The default is /backup/ (trailing slash needed). Once the backup completes, it will create a latest symlink to the most recent directory.

Leave a Comment
 
Attach a file