ftp-backup-script.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. FTP backup script
  2. =================
  3. Here is an automated script for backing up an Elgg installation.
  4. .. code:: perl
  5. #!/usr/bin/perl -w
  6. # FTP Backup
  7. use Net::FTP;
  8. # DELETE BACKUP AFTER FTP UPLOAD (0 = no, 1 = yes)
  9. $delete_backup = 1;
  10. # ENTER THE PATH TO THE DIRECTORY YOU WANT TO BACKUP, NO TRAILING SLASH
  11. $directory_to_backup = '/home/userx/public_html';
  12. $directory_to_backup2 = '/home/userx/elggdata';
  13. # ENTER THE PATH TO THE DIRECTORY YOU WISH TO SAVE THE BACKUP FILE TO, NO TRAILING SLASH
  14. $backup_dest_dir = '/home/userx/sitebackups';
  15. # BACKUP FILE NAME OPTIONS
  16. ($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
  17. $year = 1900 + $yearoffset;
  18. $site_backup_file = "$backup_dest_dir/site_backup-$day-$month-$year.tar.gz";
  19. $full_backup_file = "$backup_dest_dir/full_site_backup-$day-$month-$year.tar.gz";
  20. # MYSQL BACKUP PARAMETERS
  21. $dbhost = 'localhost';
  22. $dbuser = 'userx_elgg';
  23. $dbpwd = 'dbpassword';
  24. $mysql_backup_file_elgg = "$backup_dest_dir/mysql_elgg-$day-$month-$year.sql.gz";
  25. # ENTER DATABASE NAME
  26. $database_names_elgg = 'userx_elgg';
  27. # FTP PARAMETERS
  28. $ftp_backup = 1;
  29. $ftp_host = "FTP HOSTNAME/IP";
  30. $ftp_user = "ftpuser";
  31. $ftp_pwd = "ftppassword";
  32. $ftp_dir = "/";
  33. # SYSTEM COMMANDS
  34. $cmd_mysqldump = '/usr/bin/mysqldump';
  35. $cmd_gzip = '/usr/bin/gzip';
  36. # CURRENT DATE / TIME
  37. ($a,$d,$d,$day,$month,$yearoffset,$r,$u,$o) = localtime();
  38. $year = 1900 + $yearoffset;
  39. # BACKUP FILES
  40. $syscmd = "tar --exclude $backup_dest_dir" . "/* -czf $site_backup_file $directory_to_backup $directory_to_backup2";
  41. # elgg DATABASE BACKUP
  42. system($syscmd);
  43. $syscmd = "$cmd_mysqldump --host=$dbhost --user=$dbuser --password=$dbpwd --add-drop-table --databases $database_names_elgg -c -l | $cmd_gzip > $mysql_backup_file_elgg";
  44. system($syscmd);
  45. # CREATING FULL SITE BACKUP FILE
  46. $syscmd = "tar -czf $full_backup_file $mysql_backup_file_elgg $site_backup_file";
  47. system($syscmd);
  48. # DELETING SITE AND MYSQL BACKUP FILES
  49. unlink($mysql_backup_file_elgg);
  50. unlink($site_backup_file);
  51. # UPLOADING FULL SITE BACKUP TO REMOTE FTP SERVER
  52. if($ftp_backup == 1)
  53. {
  54. my $ftp = Net::FTP->new($ftp_host, Debug => 0)
  55. or die "Cannot connect to server: $@";
  56. $ftp->login($ftp_user, $ftp_pwd)
  57. or die "Cannot login ", $ftp->message;
  58. $ftp->cwd($ftp_dir)
  59. or die "Can't CWD to remote FTP directory ", $ftp->message;
  60. $ftp->binary();
  61. $ftp->put($full_backup_file)
  62. or warn "Upload failed ", $ftp->message;
  63. $ftp->quit();
  64. }
  65. # DELETING FULL SITE BACKUP
  66. if($delete_backup = 1)
  67. {
  68. unlink($full_backup_file);
  69. }