Automatic backup program/script for selected directories
Hello. I've searched thru the archives and couldn't find exactly what I needed. I'm looking for a program that will backup my /srv and /etc directories to a 2nd harddrive, and also my mysql database. Anything simple that could to this, maybe as a cron job?
thanks! Jonathon |
I have the following set up in my crontab:
Code:
0 23 * * fri (/usr/local/bin/backup.sh 2>/dev/null | mail -s "Backup complete" root) Code:
#!/bin/bash |
alright looks good. I did have to make 2 symlinks for it to work:
/bin/bzip2 -> /usr/bin/bzip2 /usr/bin/chmod -> /bin/chmod now does this make a new tar file every time or will it overwrite? i have a small backup drive and dont wanna quickly fill it. also, where is the mysql database stored, or how can i make a recover-able backup of it? thanks! |
Yeah, you can do it with either symlinks or just changing the script - the MySQL database files are under /var/lib/mysql by default.
Running the script multiple times on one day will overwrite the file - running it on separate days creates new files.The line containing ARCHIVE="`/bin/hostname`-`date '+%F'`.tar" does that part. Have a look at the man page for tar, you don't have to do a full backup each time. I just prefer to be able to unzip an archive and get at all the data at once. |
actually, maybe i prefer to have it maybe just mirror the /etc /srv, and /var/lib/mysql directories without even tar-ing them. Would that just be a for each KEEPER in $KEEPERS cp $KEEPER /backup -r ? Or is there somethin else i'm missing? thanks!
|
cp -dpR is a useful command for creating a copy of a directory (and recursively copying the sub-directories) with the original permissions. If you like to keep the command brief, cp -a is equivalent to cp -dpR. So yes, putting it in the loop like that will work fine.
|
Ok, changing my mind again, i think i'll keep the tar files, but will i have to manually delete them after a couple months or so, to keep that drive from getting filled? or is there an easy way to have cron do this to the old ones? like check the date and if its 2 months old delete it
|
That deletion can be incorporated into that same script. Anyways, I'm not worried about that yet, I've got space to last me a while.
How do I add that to crontab? Isn't there a command that does it? Thanks yet again |
The fastest way I know to return the list of files in a particular directory, older than a particular number of days would be the following:
Code:
find /home/backup -type f -iname '*bz2' -mtime +60 -maxdepth 1 -exec basename {} \; 2>/dev/null Code:
0 3 * * * (/usr/bin/find /home/backup -type f -iname '*bz2' -mtime +60 -maxdepth 1 -exec basename {} \; | mail -s "Deleted old files" root) |
Ahh, that was really fast! So if I edit my crontab to look like this:
Code:
0 23 * * fri (/usr/local/bin/backup.sh 2>/dev/null | mail -s "Backup complete" root) Thanks a lot, you've been a ton of help! |
No problem :) A couple of notes to hopefully explain better what it's doing. The crontab columns are:
Quote:
The second entry runs at the 0th minute of the 3rd hour every day (3am every day). The find command bits are: Code:
/home/backup Location to search |
Got it! makes perfect sense. I love how I can learn so much in such a short time. They should teach linux in high school, I swear. Thanks again!!
|
Glad to help - I wish the teachers at my son's high school felt the way you do ;)
|
Is there any way that I can check to see if my crontab is set up correctly? It doesn't appear that my backup job executed automatically.
I was thinking that there are commands to actually add it to the crontab for you? I suppose I could look all this up in the man but humans explain things a lot better! Thanks yet again! |
You can set up a dummy entry to test that cron is running. Use crontab -e or add the following to your system crontab. It will append the date to the file /tmp/cron.test every minute:
Code:
* * * * * /usr/bin/date >> /tmp/cron.test |
All times are GMT -5. The time now is 09:19 PM. |