Author Topic: [Bash] Backup  (Read 1984 times)

0 Members and 1 Guest are viewing this topic.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
[Bash] Backup
« on: August 29, 2011, 06:11:44 pm »
Code: [Select]
#!/bin/sh
FECHA=`date +%d-%m-%Y--%H-%M`

#Cleanup
rm SQL.sql
rm SQL.sql.gpg

#Backup SQL
mysqldump -A -u root -pmypass  > SQL.sql
echo pass | gpg --passphrase-fd 0 -c SQL.sql

#Backup Web
tar vczf web.tgz /www/htdocs
echo pass | gpg --passphrase-fd 0 -c web.tgz

#Upload a FTP
ftp -n -v ftp.site.com << EOT
ascii
user ca0s pwd
prompt
cd ka0labs
mkdir $FECHA
cd $FECHA
put SQL.sql.gpg
put web.tgz.gpg
bye
EOT

#Cleanup
rm SQL.sql
rm SQL.sql.gpg
rm web.tgz
rm web.tgz.gpg

Is the first thing I make in bash. I needed it to make SQL/web backups in my VPS.

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Bash] Backup
« Reply #1 on: August 30, 2011, 03:08:16 am »
cool, although maybe recommend reading passwds from stdin.. So isn't in your script.

edit: forgot to mention, read has -s option for su type input:

Code: [Select]
echo -n "enter mysql password: "; read -s sqlpasswd
Also quick tip: rm can take multiple arguments, i.e:

Code: [Select]
rm SQL.sql* web.tgz*
will replace last 4 lines
« Last Edit: August 30, 2011, 03:19:01 am by xzid »

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [Bash] Backup
« Reply #2 on: August 30, 2011, 11:57:14 am »
TY for your tips. I made it that way (password in the script) because I was going to use it as a cron task.

xor

  • Guest
Re: [Bash] Backup
« Reply #3 on: August 30, 2011, 01:37:02 pm »
If you're going to use it as a cron task, you'll be better off trying to incorporate certificates instead of passwords.

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [Bash] Backup
« Reply #4 on: August 30, 2011, 01:39:33 pm »
Could you explain more? I am too noob in this subject.

xor

  • Guest
Re: [Bash] Backup
« Reply #5 on: August 30, 2011, 02:31:50 pm »
Actually my bad, that's only if your MySQL server is hosted on another machine. Although you will need to look into a more secure method.

Anyone who is on the box can simple view the process list and see your MySQL root password passed in as an argument.


You're probably better off just backing up the MySQL data files rather than individual databases. That way you don't have to type any passwords in at all.
« Last Edit: August 30, 2011, 02:32:53 pm by xor »

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: [Bash] Backup
« Reply #6 on: August 30, 2011, 09:44:55 pm »
What if you built-in a my.cnf file in your script and piped it to mysqldump, telling mysqldump to read from stdin? Or make a real one.