It should contain:ĭepending on your setup, you may want a bunch of these for different projects/databases. cnf file contains login credentials so you don't have to pass them by the shell so they appear in ps for other users to see. Skip this section if you don't use MySQL, but the concept could carry over to other tools.ģ0 0,13 * * * /usr/bin/ssh "mysqldump -defaults-file=.my.database.cnf -uroot databasename | gzip -9c" > /path/to/databasebackup_$(date +\%Y-\%m-\%d-\%Hh\%Mm\%Ss_\%A).sql.gz The caveat is you need to make sure your mysql access can be passwordless too the safe way to do that is with. The crontab columns 00 00 * * * basically means midnight every day. If you want to back up something in your home directory, I guess you can omit -C /path/to/directory/to/backup/ because ssh will by default log you into your home directory.ĭate +\%Y-\%m-\%d-\%Hh\%Mm\%Ss_\%A will generate a timestamp like -08h58m09s_Thursday This keeps a lot of extraneous relative paths out of the archive. file_to_back_up can be a file or a directory. The -Cflag tells tar to start from a different directory than the current one. The -cf - flags and parameter create a new archive and spit the data to standard output. It can be a series of files too: file1.txt, file2.php, etc. Important: file_to_back_up is the file you are actually backing up it can be a file or directory. Have gzip write the compressed file to standard output ( c flag) and direct the output to a file on your local machine.Ġ0 00 * * * /usr/bin/ssh "sudo tar -cf -C /path/to/directory/to/backup/ file_to_back_up | gzip -9c" > /file/on/local/machine/BackUp_$(date +\%Y-\%m-\%d-\%Hh\%Mm\%Ss_\%A).tar.gz Tell ssh to execute a tar and pipe to gzip on the remote machine. This is probably the part you care about. The drawback is it's more resource-intensive, though on modern hardware I doubt this matters unless you're dealing with absurdly large files. ![]() The benefit to this is it uses bandwidth most efficiently. ![]() Make sure crontab and passwordless authentication are set up. I use crontab and passwordless authentication with ssh to archive and gzip on a remote machine and then direct the output of gzip to the local machine over ssh. However, the local machine tells the remote machine to prepare the archive. Both are pull solutions your local machine logs into a remote machine and retrieves files. Here are two examples of what I do: one for files and one for MySQL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |