Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Very Simple Backup Script
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Very Simple Backup Script

djvdorpdjvdorp Member
edited February 2012 in General

Hi,

this morning when I woke up, a thought came to my mind; a Very Simple Backup Script.
It is for one of my dedi's running on hardware raid, so the backup could just stay on the same server.

What I was thinking, was to make a cronscript (.sh I guess) which would run mysqldump and tar the
public_html directory once a day, and name them both like the current date (eg public_html-01-02-2012.tar.gz).

What would your thoughts about this be?

Regards,
Daniel

«1

Comments

  • NickMNickM Member
    edited February 2012

    Never, never, never, never, never store your only backups on the same server. I actually use rsync for my backups. I use the following script, which basically makes versioned backups of my entire /var/www directory, and hard links files that haven't changed to save space. It requires you to set up key-based authentication on your backup server, of course. I run it from a cronjob every night.

    #!/bin/bash
    date=`date "+%Y-%m-%dT%H_%M_%S"`
    user=nick
    ipaddr=127.0.0.1
    rsync -e 'ssh -i /root/.ssh/backup.id_rsa' -az \
      --delete \
      --delete-excluded \
      --link-dest=/home/$user/backups/websites/current \
      /var/www $user@$ipaddr:/home/$user/backups/websites/incomplete_$date \
      && ssh -i /root/.ssh/backup.id_rsa $user@$ipaddr \
      "mv /home/$user/backups/websites/incomplete_$date /home/$user/backups/websites/$date \
      && rm -f /home/$user/backups/websites/current \
      && ln -s /home/$user/backups/websites/$date /home/$user/backups/websites/current"
    
  • I wrote a little php script a while ago that runs using a cron every 6 hours or so and zips up my whole www directory and then renames it with a timestamp and then uploads it to a remote ftp directory.

    I can send you it if you want

    Thanked by 1djvdorp
  • @NickM

    You are THE-man. I am looking for something like this. If you google something like "versioning backup system" you get complicated stuff. If this does what you say, wonderful =) Most of my content doesn't change, so the backup process will be very fast.

    Some time ago someone mentioned a backup system with versions but I can't remember how is called.

  • Note that my script doesn't back up any databases, so you'll probably want to add something to do that if you need that functionality.

  • KuJoeKuJoe Member, Host Rep
    edited February 2012

    Took @NickM's script and made it a bit more simple (with database backup):

    #!/bin/bash
    date=`date "+%Y-%m-%dT%H_%M_%S"`
    mysqldump --opt -h localhost --user=USER --password=PASS DATABASE | gzip > /backup/mysql_$date.sql.gz
    tar -zcf /backup/home_$date.tar.gz /home
    rsync -avz -e "ssh -i /root/.ssh/SSHKEY" /backup/ USER@HOSTNAME:/backup

    Thanked by 2djvdorp yowmamasita
  • And where is the hardlinking and so? :(

  • KuJoeKuJoe Member, Host Rep

    @yomero said: And where is the hardlinking and so? :(

    None. This is a complete backup of a website/database, I don't see the point in conserving space since it's so plentiful and the biggest website I've ever backed up was only 50GB but 99% of them are less than 1GB.

  • My VPS at SecureDragon has only 15GB...

  • KuJoeKuJoe Member, Host Rep

    @yomero said: My VPS at SecureDragon has only 15GB...

    That's not a lot of data to backup. ;)

  • And isn't a lot of space to create backups...

  • KuJoeKuJoe Member, Host Rep
    edited February 2012

    @yomero said: And isn't a lot of space to create backups...

    It all depends on how you look at it. If you're backing up a website it's plenty of space. If you're backing up a fileserver then it's not a lot. My advice is if you're going to back something up, make sure your backup server has more space than the server you're backing up. ;)

  • I'm happy using rsnapshot with a custom script to dump databases and erase last day databases. Has everything I need, hard links and easy central management for all my vps

  • djvdorpdjvdorp Member
    edited February 2012

    @NickM said: Never, never, never, never, never store your only backups on the same server. I actually use rsync for my backups. I use the following script, which basically makes versioned backups of my entire /var/www directory, and hard links files that haven't changed to save space. It requires you to set up key-based authentication on your backup server, of course. I run it from a cronjob every night.

    I know backups on the same server is not good, but the point is that the www dir is like 400 mb, which is pretty much a lot to backup. The server itself has hardware raid mirroring, and I can partition it like I want. Offsite is still better though.

    @titanicsaled said: I wrote a little php script a while ago that runs using a cron every 6 hours or so and zips up my whole www directory and then renames it with a timestamp and then uploads it to a remote ftp directory.

    I can send you it if you want

    You made me curious, can you please send it to me?

    @camarg said: I'm happy using rsnapshot with a custom script to dump databases and erase last day databases. Has everything I need, hard links and easy central management for all my vps

    I know, but rsnapshot is pretty much work to setup so I thought it might be possible to do it easier.

  • Thanks all for your suggestions, scripts and time. Will make sure to try some of these suggestions out, as I really might need it one day :)

  • KuJoeKuJoe Member, Host Rep
    edited February 2012

    @djvdorp said: I know backups on the same server is not good, but the point is that the www dir is like 400 mb, which is pretty much a lot to backup.

    Why not back it up to your home network? 400MB is not a lot and if you're concerned about bandwidth don't use the date in the file name and instead use MON, TUE, WED, THUR, FRI, SAT, and SUN for the days of the week. This will provide you with 7 days worth of backups and rsync will only backup the bits that change so you will only download 400MB 1 time (download it once, then copy/rename it to the other 7 days).

    Alternatively, you can backup 400MB of data to a 10GB VPS (assuming 1GB for OS) 22 times (more if you can compress that 400MB of data). Backing up 400MB of data over a 100Mbps link wouldn't take long at all.

    Thanked by 1djvdorp
  • I use:

    #!/bin/bash
    cd /var/backup
    rm backup.10.tgz -rf
    mv backup.9.tgz backup.10.tgz
    mv backup.8.tgz backup.9.tgz
    mv backup.7.tgz backup.8.tgz
    mv backup.6.tgz backup.7.tgz
    mv backup.5.tgz backup.6.tgz
    mv backup.4.tgz backup.5.tgz
    mv backup.3.tgz backup.4.tgz
    mv backup.2.tgz backup.3.tgz
    mv backup.1.tgz backup.2.tgz
    tar cvpf backup.1.tgz files
    rsync -avz -e "ssh" host1:/var/www/ /var/backup/files/host1/www
    rsync -avz -e "ssh" host2:/var/www/ /var/backup/files/host2/www
  • KuJoeKuJoe Member, Host Rep

    Awesome. The simplest solutions are sometimes the most overlooked.

  • How did you manage to not re-invent the wheel?

  • Just use rsnapshot on backup server, and pull the files from other hosts to it. It is very easy to setup.

  • @dvjdorp

    Forgot I had it on my github repo, download it from there.

    https://github.com/titanicsaled/backuphp

  • InfinityInfinity Member, Host Rep

    I prefer rsync to be honest, ftp is a pain in the ass.

  • I just make the date command output the day of the week, and use the day of the week in the filename, so there are only always 7 backups, which get rsyncd off the box.

    mysqldump -u user -ppass dbname | gzip -9 > dbname_date +%a.sql.gz

    same for tarball of the apache folder.

  • eh.. that got screwed up.. but you get the idea.

  • @charliecron said: I just make the date command output the day of the week, and use the day of the week in the filename, so there are only always 7 backups, which get rsyncd off the box.

    how would you override that Date function? noob mode

  • Just enclose the date command in backticks.. usually the key next to your 1 key.

    The board erased the backticks.

    Thanked by 1djvdorp
  • @charliecron enclosing text in backticks is the markdown equivalent to HTML code tags. Simply escape them with a backslash like I did below :)

    mysqldump -u user -ppass dbname | gzip -9 >dbname_\`date +%a\`.sql.gz

    mysqldump -u user -ppass dbname | gzip -9 >dbname_`date +%a`.sql.gz

  • ah nice. Thanks Kuro!

  • Aren't backups fun? :)

    The best backup system (I think) is the one you are familiar with and have confidence in.

    Don't forget to periodically test or examine your backups from time to time, it's good to know they're actually valid. :)

    Thanked by 1yomero
  • @sleddog said: Don't forget to periodically test or examine your backups from time to time, it's good to know they're actually valid. :)

    100 times this. One time one of my backups was corrupted when I actually needed it, so I had to use an older version of it.

Sign In or Register to comment.