We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4420
    • 60 Posts
    Hi. I’m running modx 1.0.4 on Apache on linux centOS 5.4 with MySQL 5.0.90

    I’d like to get a nightly job running on my server to dump the contents of the modx DB to a file on the server, with generated drop table if exists statements - as produced from
    tools / backup in the manager.

    I believe the way to set this up is as a nightly cron job - but I have no experience with cron jobs.

    Does anyone have experience of doing this? Any pointers / advice / suggestions?

    Thanks
      • 19741
      • 90 Posts
      I’m using http://www.mysqldumper.net/ (this tool offers a perl-script for cron jobs and will do everything you want).
        Bye,
        Mithrandir
        • 22840
        • 1,572 Posts
        I would also recommend that if your going to backup on a daily basis you need to decide how many days you want to keep, meaning your going to have to use something that will check how many backups there are and start to delete them once it reaches that number, ie if you want 7 and it creates the 8th it will delete the oldest so that the server doesn’t get filled with backups wink
          • 32046
          • 75 Posts
          Hi Byzantium,
          Just so happens I’ve just (about a week ago) done what I think you are attempting. I looked at database backup snippets and the like but wanted something more custom. I needed a cron job to backup my database and any modx folders I specify, and email it it me twice monthly.
          I also have little knowledge about shell scripting and cron but this is what I did and it works fantastically. Those more knowledgeable will probably tear it to shreds wink
          ; The following is the shell script which you upload to root and make executable:
          #!/bin/sh
          
          # USAGE:
          # 1. Adjust variables below and upload file to /home/~username/
          # 2. Check file permissions are 700
          # 3. Setup cron job for 12:30am on 1st and 15th: 30 0 1,15 * * /bin/sh /home/USERNAME/backup-script.sh. Replace USERNAME with linux account name
          # 4. Alter variables below to suit your setup
          
          
          # website name
          website='Website ABC'
          
          # Filename prefix
          filenames='ABC'
          
          # linux account username
          username='username'
          
          #database info linuxusername followed by underslash followed by database username as in cpanel dbase settings
          dbUser='account_username'
          dbPassword='databasePassword'
          
          #Folders to zip into archieve, seperate with spaces
          # eg. foldersToZip='/home/$username/public_html/assets/css /home/$username/public_html/assets/img'
          foldersToZip="/home/$username/public_html/assets/images"
          
          #set min or max file size of files to tar and email. I set this to 150K so I don't set any monster image files which I don't really want.
          # eg -100k = tar files less than 100Kb, +1000k = tar files larger than 1000k
          maxFileSize=-150k
          
          
          ### --------------- DO NOT EDIT BELOW HERE ------------------ ###
          
          
          # first make a mysqldump of all databases to gz
          mysqldump -A --user=$dbUser --password=$dbPassword | gzip > /home/$username/mysqldump.gz
          
          
          # find all files to tar and write to file
          find $foldersToZip -size $maxFileSize -type f -print > /home/$username/filelist.txt
          
          
          # tar all files in the generated filelist.txt file
          tar -cz -T /home/$username/filelist.txt -f $filenames'_folders_'`eval date +%Y%m%d`.tgz
          
          
          #setup vars for filenames and email
          subject="Backup - $website"
          
          
          # next binary-encode attachments and pipe to email
          uuencode $filenames'_folders_'`eval date +%Y%m%d`.tgz `eval date +%Y%m%d`'_'$filenames'_files'.tgz  | mailx -s "$subject" [email protected]
          uuencode /home/$username/mysqldump.gz `eval date +%Y%m%d`'_'$filenames'_mysqldump'.gz | mailx -s "$subject" [email protected]
          
          
          # finally, delete the backup files from the server
          rm $filenames'_folders_'`eval date +%Y%m%d`.tgz
          rm /home/$username/mysqldump.gz
          rm /home/$username/filelist.txt
          


          Next add a cron job via cpanel: 30 0 1,15 * * /bin/sh /home/USERNAME/backup-script.sh.

          What this does is create a mysqldump of the specified database to root. Also a separate file is created of compressed folders, in this case /assets/images/ which is client image uploads. These files are emailed to me and then deleted from server. Takes about half a second to execute and backups appear in my inbox about 10 seconds later.

          The code is a bit messy, have not had time to go over it but it works for me smiley Oh and replace the email address in the guts of the code with your own. Should’ve made that a var too I guess sad

          Hope this helps.
            • 4420
            • 60 Posts
            This is so helpful - thanks to you all smiley